Let’s start learning how to create, copy, move(rename) delete and identify type of file by using following commands
touch,cp.mv,rm and file
Please bear in mind Linux is case sensitive. Case sensitive means that Linux will consider file examplefile and ExampleFile as different files.
Similarly /home/ali/Test is different from /home/ali/test.
[ali@rhel71 ~]$ ls -l
total 4
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Desktop
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Documents
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Downloads
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Music
drwxr-xr-x. 2 ali ali 4096 Aug 26 20:15 Pictures
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Public
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Templates
-rw-rw-r–. 1 ali ali 0 Sep 6 14:49 test
-rw-rw-r–. 1 ali ali 0 Sep 6 14:49 Test
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Videos
Above example shows that difference between two files one with uppercase T and other with lower case t
Creating file using Touch
You can create a file with one easy way. Use touch command and name of file. Below example will show you how to use it
[ali@rhel71 ~]$ touch shahid
[ali@rhel71 ~]$ touch shahid1 [ali@rhel71 ~]$ touch shahid2 [ali@rhel71 ~]$ ls -l
total 4
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Desktop
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Documents
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Downloads
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Music
drwxr-xr-x. 2 ali ali 4096 Aug 26 20:15 Pictures
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Public
-rw-rw-r–. 1 ali ali 0 Sep 6 14:53 shahid
-rw-rw-r–. 1 ali ali 0 Sep 6 14:53 shahid1
-rw-rw-r–. 1 ali ali 0 Sep 6 14:53 shahid2
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Templates
-rw-rw-r–. 1 ali ali 0 Sep 6 14:49 test
-rw-rw-r–. 1 ali ali 0 Sep 6 14:49 Test
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Videos
Copying file using cp
To copy file use cp with source and target argument. If target is directory then source file(s) are copied to that target directory
[ali@rhel71 testdir]$ touch khattak [ali@rhel71 testdir]$ lskhattak
[ali@rhel71 testdir]$ cp khattak khattak1 [ali@rhel71 testdir]$ lskhattak khattak1
In above example I copied (source file) Khattak and put it in same directory as khattak1.
If I want to create a copy in another directory then we will use below method we will specify directory
[ali@rhel71 testdir]$ pwd
/home/ali/testdir
[ali@rhel71 testdir]$ lskhattak khattak1
[ali@rhel71 testdir]$ cp khattak1 /home/ali/testdirb/ [ali@rhel71 testdir]$ cd /home/ali/testdirb [ali@rhel71 testdirb]$ lskhattak1
[ali@rhel71 testdirb]$ pwd/home/ali/testdirb
[ali@rhel71 testdirb]$In above example we are in directory /home/ali/testdir and we have two files Khattak and khattak1. We copied file khattak1 to /home/ali/testdirb.
We confirmed copy by using ls command to list content of directory
cp –r
If you have directory and it has some contents you can copy the directory and will all contents by using command cp –rls
[ali@rhel71 ~]$ mkdir myDir
file1 file2
[ali@rhel71 myDir]$ cd .. [ali@rhel71 ~]$ lsDesktop Downloads myDir Public Videos
Documents Music Pictures Templates
[ali@rhel71 ~]$ cp -r myDir myDir2 [ali@rhel71 ~]$ cd myDir2 [ali@rhel71 myDir2]$ lsfile1 file2
[ali@rhel71 myDir2]$cp –i
To prevent from overwriting existing files using switch i wicth cp
[ali@rhel71 myDir2]$ ls
file1 file2
[ali@rhel71 myDir2]$ cp file2 file3 [ali@rhel71 myDir2]$ lsfile1 file2 file3
cp: overwrite âfile3â? no
[ali@rhel71 myDir2]$To rename files use mv command
[ali@rhel71 myDir2]$ ls
file1 file2 file3
[ali@rhel71 myDir2]$ mv file3 file4 [ali@rhel71 myDir2]$ lsfile1 file2 file4
[ali@rhel71 myDir2]$To delete files use command rm
[ali@rhel71 myDir2]$ lsfile1 file2 file4
[ali@rhel71 myDir2]$ rm file1 [ali@rhel71 myDir2]$ lsfile2 file4
[ali@rhel71 myDir2]$When you remove file via command line its gone its unrecoverable be careful to use rm command
rm –i
To prevent from accidently deleting a file use rm with switch i
[ali@rhel71 myDir2]$ ls
file2 file4
[ali@rhel71 myDir2]$ rm -i file2rm: remove regular empty file âfile2â? y
[ali@rhel71 myDir2]$ lsfile4
[ali@rhel71 myDir2]$rm -rf
To delete non empty directory use rm –rf
f is for force and r means recursive it will delete all child contents(directories/files). If you will try to delete non empty directory without –rf it will not delete
[ali@rhel71 myDir2]$ ls
file4
[ali@rhel71 myDir2]$ cd .. [ali@rhel71 ~]$ rm myDir2rm: cannot remove âmyDir2â: Is a directory
[ali@rhel71 ~]$ rm -rf myDir2 [ali@rhel71 ~]$ lsdrwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Desktop
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Documents
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Downloads
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Music
drwxrwxr-x. 2 ali ali 30 Sep 6 15:10 myDir
drwxr-xr-x. 2 ali ali 4096 Aug 26 20:15 Pictures
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Public
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Templates
drwxr-xr-x. 2 ali ali 6 Aug 9 11:56 Videos
myDir2 has been deleted.
File
Unlinke windows linux does not use extension to determine file type if you as system administrator want to determine type of file use command file and name of file
[ali@rhel71 Documents]$ lsmynotes.txt
[ali@rhel71 Documents]$ file mynotes.txtmynotes.txt: ASCII text
[ali@rhel71 Documents]$I hope you have learnt and enjoyed this tutorial. Please let me know your thoughts and feedback in comments thanks