Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Linux - Working with files

In this session we have covered, how to recognize, create, remove, copy and move files using commands like file, touch, rm, cp, mv and rename.

all files are case sensitive

Linux is case sensitive, this means that file1 is different from FILE1, and /user/data is different from /user/Data. This following command shows the difference between two files, one with upper case S, the other with lower case s.

[email protected]:~$ vim summer.txt
[email protected]:~$ cat summer.txt
the quick brown fox jumps over the lazy dog.
[email protected]:~$ vim Summer.txt
[email protected]:~$ cat Summer.txt
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
[email protected]:~$

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 tutorial that everything on Linux is a file.

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.

[email protected]:~$ file linux-command-past-date.png
linux-command-past-date.png: PNG image data, 397 x 219, 8-bit/color RGB, non-interlaced
[email protected]:~$ file Summer.txt
Summer.txt: ASCII text
[email protected]:~$

The file command uses a magic file that contains patterns to recognize 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.

[email protected]:/home$ file /dev/sda
/dev/sda: block special 
[email protected]:/home$ file -s /dev/sda
/dev/sda: no read permission
[email protected]:/home$ file /proc/cpuinfo
/proc/cpuinfo: empty 
[email protected]:/home$ file -s /proc/cpuinfo
/proc/cpuinfo: ASCII text, with very long lines

touch

You can use the touch command to create an empty file. The following touch commands create two files, file1 and file2 with a size of zero bytes.

[email protected]:~$ touch file1
[email protected]:~$ touch file2
[email protected]:~$ ls -l
total 104
-rw-rw-r-- 1 datasoft datasoft   729 Jul 28 15:34 abc.txt
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Desktop
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Documents
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Downloads
-rw-r--r-- 1 datasoft datasoft  8980 Jun 20 11:42 examples.desktop
-rw-rw-r-- 1 datasoft datasoft     0 Jul 29 12:32 file1
-rw-rw-r-- 1 datasoft datasoft     0 Jul 29 12:32 file2

touch -t

Using 't' option you can set the file's modification time (format [[CC]YY]MMDDhhmm[.ss]).

[email protected]:~$ touch -t 200505050000 SinkoDeMayo
[email protected]:~$ touch -t 130207111630 BigBattle
touch: invalid date format ?130207111630?
[email protected]:~$ ls -l
total 104
-rw-rw-r-- 1 datasoft datasoft   729 Jul 28 15:34 abc.txt
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Desktop
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Documents
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Downloads
-rw-r--r-- 1 datasoft datasoft  8980 Jun 20 11:42 examples.desktop
-rw-rw-r-- 1 datasoft datasoft     0 Jul 29 12:32 file1
-rw-rw-r-- 1 datasoft datasoft     0 Jul 29 12:32 file2
-rw------- 1 datasoft datasoft  8800 Jan 27  2014 linux-command-past-date.png
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Music
drwxrwxr-x 6 datasoft datasoft  4096 Jul 28 17:01 MyDir
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Pictures
drwxr-xr-x 2 datasoft datasoft  4096 Jun 20 12:41 Public
-rw-rw-r-- 1 datasoft datasoft     0 May  5  2005 SinkoDeMayo

rm

The rm (short for "remove") command is used to remove a file. 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!

[email protected]:~$ ls
abc.txt    examples.desktop             Music     SinkoDeMayo  test3
Desktop    file1                        MyDir     summer.txt   typescript
Documents  file2                        Pictures  Summer.txt   Videos
Downloads  linux-command-past-date.png  Public    Templates
[email protected]:~$ touch -t 200505050000 SinkoDeMayo
[email protected]:~$ rm SinkoDeMayo
[email protected]:~$ ls
abc.txt    examples.desktop             Music     summer.txt  typescript
Desktop    file1                        MyDir     Summer.txt  Videos
Documents  file2                        Pictures  Templates
Downloads  linux-command-past-date.png  Public    test3

rm -i

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

[email protected]:~$ touch brel.txt
[email protected]:~$ rm -i brel.txt
rm: remove regular empty file ?brel.txt?? y
[email protected]:~$ 

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.

[email protected]:~$ ls test3
Mytest
[email protected]:~$ rm test3
rm: cannot remove ?test3?: Is a directory
[email protected]:~$ rm -rf test3
[email protected]:~$ ls test3
ls: cannot access test3: No such file or directory

cp

The cp command is used to copy one file to another. If the target is a directory, then the source files are copied to that target directory. The command works almost same as the copy command in Microsoft operating systems :

[email protected]:~$ touch FileA
[email protected]:~$ ls
abc.txt           file1                        MyDir       Templates
Desktop           file2                        Pictures    typescript
Documents         FileA                        Public      Videos
Downloads         linux-command-past-date.png  summer.txt
examples.desktop  Music                        Summer.txt
[email protected]:~$ cp FileA FileB
[email protected]:~$ ls
abc.txt           file1                        Music       Summer.txt
Desktop           file2                        MyDir       Templates
Documents         FileA                        Pictures    typescript
Downloads         FileB                        Public      Videos
examples.desktop  linux-command-past-date.png  summer.txt
[email protected]:~$ mkdir MyTest
[email protected]:~$ ls
abc.txt           file1                        Music     summer.txt
Desktop           file2                        MyDir     Summer.txt
Documents         FileA                        MyTest    Templates
Downloads         FileB                        Pictures  typescript
examples.desktop  linux-command-past-date.png  Public    Videos
[email protected]:~$ mkdir MyDir1
[email protected]:~$ ls
abc.txt           file2                        MyDir1      Templates
Desktop           FileA                        MyTest      typescript
Documents         FileB                        Pictures    Videos
Downloads         linux-command-past-date.png  Public
examples.desktop  Music                        summer.txt
file1             MyDir                        Summer.txt
[email protected]:~$ cp FileA MyDir1/
[email protected]:~$ ls MyDir1/
FileA

cp -r

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

[email protected]:~$ ls
abc.txt           file2                        MyDir1      Templates
Desktop           FileA                        MyTest      typescript
Documents         FileB                        Pictures    Videos
Downloads         linux-command-past-date.png  Public
examples.desktop  Music                        summer.txt
file1             MyDir                        Summer.txt
[email protected]:~$ ls MyDir1/
FileA
[email protected]:~$ cp -r MyDir1 MyDirA
[email protected]:~$ ls
abc.txt           file2                        MyDir1      Summer.txt
Desktop           FileA                        MyDirA      Templates
Documents         FileB                        MyTest      typescript
Downloads         linux-command-past-date.png  Pictures    Videos
examples.desktop  Music                        Public
file1             MyDir                        summer.txt
[email protected]:~$ ls MyDirA
FileA

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.

[email protected]:~$ cp file2 FileA MyDir1

cp -i

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

[email protected]:~$ cp FileA FileB
[email protected]:~$ cp -i FileA FileB
cp: overwrite ?FileB?? no
[email protected]:~$

cp -p

To preserve permissions and time stamps from source files, use cp -p.

[email protected]:~$ cd MyDir1
[email protected]:~/MyDir1$ ls
file1  file2  FileA
[email protected]:~/MyDir1$ ls -l
total 0
-rw-rw-r-- 1 datasoft datasoft 0 Jul 29 13:30 file1
-rw-rw-r-- 1 datasoft datasoft 0 Jul 29 13:30 file2
-rw-rw-r-- 1 datasoft datasoft 0 Jul 29 13:22 FileA
[email protected]:~/MyDir1$ cp * /
cp: cannot create regular file ?/file1?: Permission denied
cp: cannot create regular file ?/file2?: Permission denied
cp: cannot create regular file ?/FileA?: Permission denied
[email protected]:~/MyDir1$ cp * ../MyTest/
[email protected]:~/MyDir1$ cd ..
[email protected]:~$ cd MyTest
[email protected]:~/MyTest$ ls -l
total 0
-rw-rw-r-- 1 datasoft datasoft 0 Jul 29 13:34 file1
-rw-rw-r-- 1 datasoft datasoft 0 Jul 29 13:34 file2
-rw-rw-r-- 1 datasoft datasoft 0 Jul 29 13:34 FileA

mv

The mv command is used to rename a file or to move the file to another directory.

[email protected]:~$ touch file100
[email protected]:~$ ls
abc.txt           file100                      MyDir     summer.txt
Desktop           file2                        MyDir1    Summer.txt
Documents         FileA                        MyDirA    Templates
Downloads         FileB                        MyTest    typescript
examples.desktop  linux-command-past-date.png  Pictures  Videos
file1             Music                        Public
[email protected]:~$ mv file100 ABC.txt
[email protected]:~$ ls
abc.txt           file1                        MyDir     summer.txt
ABC.txt           file2                        MyDir1    Summer.txt
Desktop           FileA                        MyDirA    Templates
Documents         FileB                        MyTest    typescript
Downloads         linux-command-past-date.png  Pictures  Videos
examples.desktop  Music                        Public
[email protected]:~$

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

rename

The rename command can also be used but it has a more complex syntax to enable renaming of many files at once. Below are two examples, the first switches all occurrences of txt to png for all file names ending in .txt. The second example switches all occurrences of upper case 'ABC' in lower case 'abc' for all file names ending in .png . The following syntax will work on Debian and Ubuntu (prior to Ubuntu 7.10).

[email protected]:~$ ls -l *.txt
-rw-r--r-- 1 root root 23 Jul 29 14:20 myfile1.txt
-rw-r--r-- 1 root root  9 Jul 29 14:20 myfile2.txt
[email protected]:~$ rename 's/txt/doc/' *.txt
[email protected]:~$ ls -l *.doc
-rw-r--r-- 1 root root 23 Jul 29 14:20 myfile1.doc
-rw-r--r-- 1 root root  9 Jul 29 14:20 myfile2.doc
[email protected]:~$ rename 's/myfile/MYFILE/' *.doc
[email protected]:~$ ls -l *.doc
-rw-r--r-- 1 root root 23 Jul 29 14:20 MYFILE1.doc
-rw-r--r-- 1 root root  9 Jul 29 14:20 MYFILE2.doc
[email protected]:~$

On Red Hat Enterprise Linux (and many other Linux distributions like Ubuntu 8.04), the syntax of rename is a bit different. The first example below renames all *.conf files replacing any occurrence of conf with bak. The second example renames all (*) files replacing one with ONE.

Exercise, Practice and Solution :

1. List the files in the /bin directory.

Code:

ls /bin

2. Display the type of file of /bin/cat, /etc/passwd and /usr/bin/passwd.

Code:

sudo file /bin/cat /etc/passwd /usr/bin/passwd

3. Download wolf.jpg and LinuxFun.pdf from https://linux-training.be (wget https:// linux-training.be/files/studentfiles/abc.jpg and wget https://linux-training.be/files/books/xyz.pdf)

Code:

sudo wget https://linux-training.be/files/studentfiles/abc.jpg
sudo wget https://linux-training.be/files/studentfiles/abc.png
sudo wget https://linux-training.be/files/books/xyz.pdf

4. Display the type of file of abc.jpg and xyz.pdf.

Code:

file abc.jpg xyz.pdf

5. Rename abc.jpg to xyz.pdf (use mv).

Code:

sudo mv abc.jpg xyz.pdf

6. Display the type of file of abc.pdf and xyz.pdf.

Code:

file wolf.pdf LinuxFun.pdf

7. Create a directory ~/workarea and enter it.

Code:

mkdir ~/woorkarea ; cd ~/workarea

8. Create the files abc.txt and xyz.txt in touched.

Code:

touch abc.txt xyz.txt

9. Change the date on yesterday.txt to match yesterday's date.

Code:

touch -t 200810251405 yesterday.txt (substitute 20081025 with yesterday)

10. Copy abc.txt to copy.xyz.txt

Code:

cp abc.txt copy.xyz.txt

11. Rename copy.abc.txt to kim

Code:

mv copy.abc.txt kim

12. Create a directory called ~/testbackup and copy all files from ~/touched into it.

Code:

mkdir ~/testbackup ; cp -r ~/touched ~/testbackup/

13. Use one command to remove the directory ~/testbackup and all files into it.

Code:

rm -rf ~/testbackup

14. Create a directory ~/etcbackup and copy all *.conf files from /etc into it. Did you include
all subdirectories of /etc?

Code:

sudo cp -r /etc/*.conf /home/datasoft/touched/etcbackup
Only *.conf files that are directly in /etc/ are copied.

15. Use rename to rename all *.conf files to *.backup . (if you have more than one distro
available, try it on all!)

Code:

On RHEL: touch 1.conf 2.conf ; rename conf backup *.conf
On Debian: touch 1.conf 2.conf ; rename 's/conf/backup/' *.conf

Previous: Linux - Working with directories
Next: Linux - Working with files contents