working with files

All files are case sensitive

Linux is case sensitive, this means that FILE1 is different from file1, and /etc/hosts is different from /etc/Hosts (the latter one does not exist on a typical Linux computer).
This screenshot shows the difference
root@linux:~/Linux$ ls
winter.txt Winter.txt
root@linux:~/Linux$ cat winter.txt
It is cold.
root@linux:~/Linux$ cat Winter.txt
It is very cold!
everything is a file

A directory is a special kind of file, but it is still a (case sensitive!) file. Even a
terminal window (/dev/pts/4) or a hard disk (/dev/sdb) is represented somewhere in
the file system as a file. It will become clear throughout this course that everything
on Linux is a file.

The file utility determines the file type. Linux does not use extensions to determine
the file type. Your editor does not care whether a file ends in .TXT or .DOC. As a
system administrator, you should use the file command to determine the file type.
Here are some examples on a typical Linux system.
root@linux:~$ file pic33.png
pic33.png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced
root@linux:~$ file /etc/passwd
/etc/passwd: ASCII text
root@linux:~$ file HelloWorld.c
HelloWorld.c: ASCII C program text

The file command uses a magic file that contains patterns to recognise file types.
The magic file is located in /usr/share/file/magic. Type man 5 magic for more information.
It is interesting to point out file -s for special files like those in /dev and /proc.
root@debian6~# file /dev/sda
/dev/sda: block special
root@debian6~# file -s /dev/sda
/dev/sda: x86 boot sector; partition 1: ID=0x83, active, starthead...
root@debian6~# file /proc/cpuinfo
/proc/cpuinfo: empty
root@debian6~# file -s /proc/cpuinfo
/proc/cpuinfo: ASCII C++ program text

One easy way to create a file is with touch.
root@linux:~/test$ touch file1
root@linux:~/test$ touch file2
root@linux:~/test$ touch file555
root@linux:~/test$ ls -l
total 0
-rw-r--r-- 1 paul paul 0 2007-01-10 21:40 file1
-rw-r--r-- 1 paul paul 0 2007-01-10 21:40 file2
-rw-r--r-- 1 paul paul 0 2007-01-10 21:40 file555
#touch –t

Of course, touch can do more than just create files. Can you determine what by
looking at the next screenshot? If not, check the manual for touch.
root@linux:~/test$ touch -t 200505050000 SinkoDeMayo
root@linux:~/test$ touch -t 130207111630 BigBattle
root@linux:~/test$ ls -l
total 0
-rw-r--r-- 1 paul paul 0 1302-07-11 16:30 BigBattle
-rw-r--r-- 1 paul paul 0 2005-05-05 00:00 SinkoDeMayo

When you no longer need a file, use rm to remove it. Unlike some graphical user
interfaces, the command line in general does not have a waste bin or trash can to
recover files. When you use rm to remove a file, the file is gone. Therefore, be careful
when removing files!
root@linux:~/test$ ls
BigBattle SinkoDeMayo
root@linux:~/test$ rm BigBattle
root@linux:~/test$ ls
#rm –i

To prevent yourself from accidentally removing a file, you can type rm -i.
root@linux:~/Linux$ touch brel.txt
root@linux:~/Linux$ rm -i brel.txt
rm: remove regular empty file `brel.txt'? y
#rm –rf  

By default, rm -r will not remove non-empty directories. However rm accepts several
options that will allow you to remove any directory. The rm -rf statement is famous
because it will erase anything (providing that you have the permissions to do so).
When you are logged on as root, be very careful with rm -rf (the f means force and
the r means recursive) since being root implies that permissions don't apply to you.
You can literally erase your entire file system by accident.
root@linux:~$ ls test
root@linux:~$ rm test
rm: cannot remove `test': Is a directory
root@linux:~$ rm -rf test
root@linux:~$ ls test
ls: test: No such file or directory

To copy a file, use cp with a source and a target argument. If the target is a directory,
then the source files are copied to that target directory.
root@linux:~/test$ touch FileA
root@linux:~/test$ ls
root@linux:~/test$ cp FileA FileB
root@linux:~/test$ ls
FileA FileB
root@linux:~/test$ mkdir MyDir
root@linux:~/test$ ls
FileA FileB MyDir
root@linux:~/test$ cp FileA MyDir/
root@linux:~/test$ ls MyDir/
#cp –r   

To copy complete directories, use cp -r (the -r option forces recursive copying of
all files in all subdirectories).
root@linux:~/test$ ls
FileA FileB MyDir
root@linux:~/test$ ls MyDir/
root@linux:~/test$ cp -r MyDir MyDirB
root@linux:~/test$ ls
FileA FileB MyDir MyDirB
root@linux:~/test$ ls MyDirB
cp multiple files to directory

You can also use cp to copy multiple files into a directory. In this case, the last
argument (a.k.a. the target) must be a directory.
cp file1 file2 dir1/file3 dir1/file55 dir2
#cp –I    

To prevent cp from overwriting existing files, use the -i (for interactive) option.
root@linux:~/test$ cp fire water
root@linux:~/test$ cp -i fire water
cp: overwrite `water'? no
#cp –p  

To preserve permissions and time stamps from source files, use cp -p.
root@linux:~/perms$ cp file* cp
root@linux:~/perms$ cp -p file* cpp
root@linux:~/perms$ ll *
-rwx------ 1 paul paul 0 2008-08-25 13:26 file33
-rwxr-x--- 1 paul paul 0 2008-08-25 13:26 file42
total 0
-rwx------ 1 paul paul 0 2008-08-25 13:34 file33
-rwxr-x--- 1 paul paul 0 2008-08-25 13:34 file42
total 0
-rwx------ 1 paul paul 0 2008-08-25 13:26 file33
-rwxr-x--- 1 paul paul 0 2008-08-25 13:26 file42

Use mv to rename a file or to move the file to another directory.
root@linux:~/test$ touch file100
root@linux:~/test$ ls
root@linux:~/test$ mv file100 ABC.txt
root@linux:~/test$ ls
When you need to rename only one file then mv is the preferred command to use.

No comments:

Post a Comment