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.

datasoft@datasoft-linux:~$ vim summer.txt
datasoft@datasoft-linux:~$ cat summer.txt
the quick brown fox jumps over the lazy dog.
datasoft@datasoft-linux:~$ vim Summer.txt
datasoft@datasoft-linux:~$ cat Summer.txt
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
datasoft@datasoft-linux:~$

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.

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

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.

datasoft@datasoft-linux:/home$ file /dev/sda
/dev/sda: block special 
datasoft@datasoft-linux:/home$ file -s /dev/sda
/dev/sda: no read permission
datasoft@datasoft-linux:/home$ file /proc/cpuinfo
/proc/cpuinfo: empty 
datasoft@datasoft-linux:/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.

datasoft@datasoft-linux:~$ touch file1
datasoft@datasoft-linux:~$ touch file2
datasoft@datasoft-linux:~$ 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]).

datasoft@datasoft-linux:~$ touch -t 200505050000 SinkoDeMayo
datasoft@datasoft-linux:~$ touch -t 130207111630 BigBattle
touch: invalid date format ?130207111630?
datasoft@datasoft-linux:~$ 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!

datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ touch -t 200505050000 SinkoDeMayo
datasoft@datasoft-linux:~$ rm SinkoDeMayo
datasoft@datasoft-linux:~$ 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.

datasoft@datasoft-linux:~$ touch brel.txt
datasoft@datasoft-linux:~$ rm -i brel.txt
rm: remove regular empty file ?brel.txt?? y
datasoft@datasoft-linux:~$ 

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.

datasoft@datasoft-linux:~$ ls test3
Mytest
datasoft@datasoft-linux:~$ rm test3
rm: cannot remove ?test3?: Is a directory
datasoft@datasoft-linux:~$ rm -rf test3
datasoft@datasoft-linux:~$ 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 :

datasoft@datasoft-linux:~$ touch FileA
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ cp FileA FileB
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ mkdir MyTest
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ mkdir MyDir1
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ cp FileA MyDir1/
datasoft@datasoft-linux:~$ ls MyDir1/
FileA

cp -r

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

datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ ls MyDir1/
FileA
datasoft@datasoft-linux:~$ cp -r MyDir1 MyDirA
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ 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.

datasoft@datasoft-linux:~$ cp file2 FileA MyDir1

cp -i

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

datasoft@datasoft-linux:~$ cp FileA FileB
datasoft@datasoft-linux:~$ cp -i FileA FileB
cp: overwrite ?FileB?? no
datasoft@datasoft-linux:~$

cp -p

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

datasoft@datasoft-linux:~$ cd MyDir1
datasoft@datasoft-linux:~/MyDir1$ ls
file1  file2  FileA
datasoft@datasoft-linux:~/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
datasoft@datasoft-linux:~/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
datasoft@datasoft-linux:~/MyDir1$ cp * ../MyTest/
datasoft@datasoft-linux:~/MyDir1$ cd ..
datasoft@datasoft-linux:~$ cd MyTest
datasoft@datasoft-linux:~/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.

datasoft@datasoft-linux:~$ touch file100
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ mv file100 ABC.txt
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$

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).

datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ rename 's/txt/doc/' *.txt
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$ rename 's/myfile/MYFILE/' *.doc
datasoft@datasoft-linux:~$ 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
datasoft@datasoft-linux:~$

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 http://linux-training.be (wget http:// linux-training.be/files/studentfiles/abc.jpg and wget http://linux-training.be/files/books/xyz.pdf)

Code:

sudo wget http://linux-training.be/files/studentfiles/abc.jpg
sudo wget http://linux-training.be/files/studentfiles/abc.png
sudo wget http://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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/linux-system-administration/working-with-files.php