working with files

In this post we learn how to recognise, create, remove, copy and move files using commands like file, touch, rm, cp, mv and rename.

all files are case sensitive

Files on Linux (or any Unix) are 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 between two files, one with upper case W, the other with lower case w.
kick@centos:~/Linux$ ls
winter.txt Winter.txt
kick@centos:~/Linux$ cat winter.txt
It is cold.
kick@centos:~/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. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1) and any process are all represented somewhere in the file system as a file. It will become clear throughout this course that everything on Linux is a file.

kick@centos:~$ file pic33.png
pic33.png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced
kick@centos:~$ file /etc/passwd
/etc/passwd: ASCII text
kick@centos:~$ 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@localhost~# file /dev/sda
/dev/sda: block special
root@localhost~# file -s /dev/sda
/dev/sda: x86 boot sector; partition 1: ID=0x83, active, starthead…
root@localhost~# file /proc/cpuinfo
/proc/cpuinfo: empty
root@localhost~# file -s /proc/cpuinfo
/proc/cpuinfo: ASCII C++ program text

touch

create an empty file

One easy way to create an empty file is with touch. (We will see many other ways forncreating files later in this book.)
This starts with an empty directory, creates two files with touch and the lists those files.
root@centos:~$ ls -l
total 0
kick@centos:~$ touch file42
kick@centos:~$ touch file33
kick@centos:~$ ls -l
total 0
-rw-r–r– 1 paul paul 0 Oct 15 08:57 file33
-rw-r–r– 1 paul paul 0 Oct 15 08:56 file42
kick@centos:~$

touch -t

The touch command can set some properties while creating empty files. Can you determine what is set by looking at the next screenshot? If not, check the manual for touch.
kick@centos:~$ touch -t    200505050000 SinkoDeMayo
kick@centos:~$ touch -t    130207111630 BigBattle.txt
kick@centos:~$ ls -l
total 0
-rw-r–r– 1 paul paul 0      Jul 11 1302 BigBattle.txt
-rw-r–r– 1 paul paul 0      Jan 15 08:57 file33
-rw-r–r– 1 paul paul 0      Jan 15 08:56 file42
-rw-r–r– 1 paul paul 0      May 5 2019 SinkoDeMayo
kick@centos:~$

rm

remove forever

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@localhost:~$ ls
BigBattle.txt file33 file42 SinkoDeMayo
root@localhost:~$ rm BigBattle.txt
root@localhost:~$ ls
file33 file42 SinkoDeMayo
root@localhost:~$

rm -i

To prevent yourself from accidentally removing a file, you can type rm -i.

kick@centos:~$ ls
file33 file42 SinkoDeMayo
kick@centos:~$ rm -i file33
rm: remove regular empty file `file33′? yes
kick@centos:~$ rm -i SinkoDeMayo
rm: remove regular empty file `SinkoDeMayo’? n
kick@centos:~$ ls
file42 SinkoDeMayo
kick@centos:~$

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.

kick@centos:~$ mkdir test
kick@centos:~$ rm test
rm: cannot remove `test’: Is a directory
kick@centos:~$ rm -rf test
kick@centos:~$ ls test
ls: cannot access test: No such file or directory
kick@centos:~$

cp

copy one file

To copy a file, use cp with a source and a target argument.

kick@centos:~$ ls
file42 SinkoDeMayo
kick@centos:~$ cp file42 file42.copy
kick@centos:~$ ls
file42 file42.copy SinkoDeMayo

copy to another directory

If the target is a directory, then the source files are copied to that target directory.

kick@centos:~$ mkdir dir42
kick@centos:~$ cp SinkoDeMayo dir42
kick@centos:~$ ls dir42/
SinkoDeMayo

cp -r

To copy complete directories, use cp -r (the -r option forces recursive copying of all files in all subdirectories).

kick@centos:~$ ls
dir42 file42 file42.copy SinkoDeMayo
kick@centos:~$ cp -r dir42/ dir33
kick@centos:~$ ls
dir33 dir42 file42 file42.copy SinkoDeMayo
kick@centos:~$ ls dir33/
SinkoDeMayo

copy 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.
kick@centos:~$ cp file42 file42.copy SinkoDeMayo dir42/
kick@centos:~$ ls dir42/
file42 file42.copy SinkoDeMayo

cp -i

To prevent cp from overwriting existing files, use the -i (for interactive) option.

kick@centos:~$ cp SinkoDeMayo file42
kick@centos:~$ cp SinkoDeMayo file42
kick@centos:~$ cp -i SinkoDeMayo file42
cp: overwrite `file42′? n
kick@centos:~$

mv

rename files with mv

Use mv to rename a file or to move the file to another directory.
root@localhost:~$ ls
dir33 dir42 file42 file42.copy                     SinkoDeMayo
root@localhost:~$ mv file42 file33
root@localhost:~$ ls
dir33 dir42 file33 file42.copy                     SinkoDeMayo
root@localhost:~$

When you need to rename only one file then mv is the preferred command to use.

rename directories with mv

The same mv command can be used to rename directories.
root@localhost:~$ ls -l
total 8
drwxr-xr-x 2 paul paul 4096 Oct   19   09:36  dir33
drwxr-xr-x 2 paul paul 4096 Oct   19   09:36  dir42
-rw-r–r– 1 paul paul          0 Oct   19   09:38  file33
-rw-r–r– 1 paul paul          0 Oct   19   09:16  file42.copy
-rw-r–r– 1 paul paul          0 May  5    2019    Sinkodemayo
root@localhost:~$ mv dir33 backup
root@localhost:~$ ls -l
total 8
drwxr-xr-x 2 paul paul 4096 Oct   19 09:36 Backup
drwxr-xr-x 2 paul paul 4096 Oct   19 09:36 dir42
-rw-r–r– 1 paul paul          0 Oct   19 09:38 file33
-rw-r–r– 1 paul paul          0 Oct   19 09:16 file42.copy
-rw-r–r– 1 paul paul          0 May  5  2019   Sinkodemayo
root@localhost:~$

mv -i

The mv also has a -i switch similar to cp and rm.

root@localhost:~$ mv -i file33 SinkoDeMayo
mv: overwrite `SinkoDeMayo’? no
root@localhost:~$

rename

rename on Debian/Ubuntu

The rename command on Debian uses regular expressions to rename many files at once.
Below a rename example that switches all occurrences of txt to png for all file names ending in .txt.

root@localhost:~/test42$ ls
abc.txt file33.txt file42.txt
root@localhost:~/test42$ rename ‘s/\.txt/\.png/’ *.txt
root@localhost:~/test42$ ls
abc.png file33.png file42.png

This second example switches all (first) occurrences of file into document for all file names ending in .png.
root@localhost:~/test42$ ls
abc.png file33.png file42.png
root@localhost:~/test42$ rename ‘s/file/document/’ *.png
root@localhost:~/test42$ ls
abc.png document33.png document42.png
root@localhost:~/test42$

rename on CentOS/RHEL/Fedora

On Red Hat Enterprise Linux, the syntax of rename is a bit different. The example below renames all *.conf files replacing any occurrence of .conf with .backup.
[root@localhost ~]$ touch one.conf two.conf three.conf
[root@localhost ~]$ rename .conf .backup *.conf
[root@localhost ~]$ ls
one.backup three.backup two.backup
[root@localhost ~]$

The second example all (*) files replacing one with ONE.
[root@localhost ~]$ ls
one.backup three.backup two.backup
[root@localhost ~]$ rename one ONE *
[root@localhost ~]$ ls
ONE.backup three.backup two.backup
[root@localhost ~]$

For any query or issue, feel free to discuss on http://discuss.eduguru.in