31 December 2011

"rm" Command in Linux/Unix

Leave a Comment
"rm" Command in Linux/Unix


"rm"  Command : 
                             rm command is mainly  used for deleting files and folders in Linux/Unix. It is just like delete option in Windows.


Syntax For rm Command :


    rm [-options] file(s)/directory


Example:
$ touch file1
$ ls -l file1
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 11:07 file1
$ rm file1
$ ls -l file1
ls: cannot access file1: No such file or directory
$

  In above Example file1 was created using touch command and then deleted using rm command .




Options used with rm command :


1. -i :  This is used for interactive removal of file/directory . rm command will prompts for conformation before removing any file/directory.


Syntax:


          rm  -i   file(s)/directory


Example:-
$ touch file1
$ ls -l file1
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 11:09 file1
$ rm -i file1
rm: remove regular empty file `file1'? y
$ ls -l file1
ls: cannot access file1: No such file or directory
$

Another Example where we are Removing Multiple files.
$ touch file1 file2 file3 file4
$ ls -l file1 file2 file3 file4
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 11:14 file1
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 11:14 file2
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 11:14 file3
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 11:14 file4
$ rm -i file1 file2 file3 file4
rm: remove regular empty file `file1'? y
rm: remove regular empty file `file2'? y
rm: remove regular empty file `file3'? y
rm: remove regular empty file `file4'? y
$ ls -l file1 file2 file3 file4
ls: cannot access file1: No such file or directory
ls: cannot access file2: No such file or directory
ls: cannot access file3: No such file or directory
ls: cannot access file4: No such file or directory
In this Example we have created more than one file and deleted them .




2. -r or -R
                    Recursively deleted directory and sub-directories.


*directory and sub-directories cannot be removed using simply rm command.
$ mkdir sandeep1
$ rm sandeep
rm: cannot remove `sandeep': Is a directory
$




  a. Removing Directory using -r option 

   Syntax:
                
             rm  -ri  directoryname


 * options can be combined together in linux.


Example:
$ mkdir sandeep1
$ cd sandeep1
$ touch file1
$ cd ..
$ rm -ri sandeep1
rm: descend into directory `sandeep1'? y
rm: remove regular empty file `sandeep1/file1'? y
rm: remove directory `sandeep1'? y
$

We have created a directory called sandeep1 and a file called file1 inside this directory and deleted using  -r option (note that i is used here only for interactivity , you can use -r option without i).




 b. Removing Directory using -R option 


 Syntax:
                
             rm  -iR  directoryname  or  rm -i  directoryname -R


Example:
$ cd sandeep1
cd: 51: can't cd to sandeep1
$ mkdir sandeep1
$ cd sandeep
$ touch file1
$ cd ..
$ rm -Ri sandeep1
rm: remove directory `sandeep1'? y
$


$ cd sandeep1
cd: 16: can't cd to sandeep1
$ mkdir sandeep1
$ cd sandeep1
$ touch sandeep
$ cd ..
$ rm -i sandeep1 -R
rm: descend into directory `sandeep1'? y
rm: remove regular empty file `sandeep1/sandeep'? y
rm: remove directory `sandeep1'? y
$


note that rmdir command  is also used to remove directory , but the condition is that the  directory shoud be empty .




3. -f :
         Remove all files  in a directory without prompting the user(Forcefully).This will not ask you “are you sure you want to remove [file]".


Syntax:


        rm  -rf   directoryname


Example:
$ cd sandeep1
cd: 60: can't cd to sandeep1
$ mkdir sandeep1
$ cd sandeep1
$ touch file1
$ ls
file1
$ cd ..
$ rm -rf sandeep1
$ cd sandeep1
cd: 67: can't cd to sandeep1
$




Some Important Points Regarding  "rm" command


1. Always use -i option with rm command to avoid accidental removal of file/directory.
2. Never use "rm  -rf  / " , it will remove your entire Linux system without warning.
3. rm command can use Multiple file name and directory name.






If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment