Sunday 29 January 2012

rm


rm

rm  is used to remove files or directories. It can be  used interactively to ask user input while removing  multiple files or  the contents of a  directory.

A great care has to be taken before running rm command. There is no way to recover the files which are removed unless they were backed up on tape.

Use rm <filename> to remove these files.

Example 1)
To remove multiple files with a common keyword.

/home/jenny$ls   city_DETAIL*.lst
City_DETAIL_MUM.lst
City_DETAIL_DEL.lst
City_DETAIL_BLR.lst
City_DETAIL_KOL.lst
City_DETAIL-ALL
..
To remove all these files except  City_DETAIL-ALL   enter
/home/jenny$rm  City_DETAIL_???.lst

If the  file does not belong to the current user, or if the file does not have write permissions, the
rm command asks you before removing ,i:e

rm: remove City_DETAIL_KOL.lst??.lst

If you do not want to answer to  this  question to all the files, use –f option to remove forcefully.

rm  -f  City_DETAIL_???.lst

Example 2)
To remove directories and files use –rf option
/home/jenny$rm  -rf   FILMS
This will remove all the  sub directories of  and the directory itself. This command is also same as
cd  /; rm –rf  home/jenny/FILMS

remember that you cannot remove a directory  if  it  is  the  path of  your working directory or a parent when you are inside its subdirectories.
eg: both of these  don’t work.

/home/jenny /FILMS$ rm   -rf    /home/jenny /FILMS             #Does not work
/home/jenny /FILMS/K3g$rm   -rf   /home/jenny /FILMS   #Does not work




To view the files which are being removed when files are removed in bulk.

Suppose you have a file containing the list of files to be removed. then
/home/jenny$ head file_to_remove_list
Jj12_2.txt
Df003.log
Awer_cat.lst
Sdifre.post
Mail34.log
TAB/Hourwise_list.doc
Sequence.log
Created/Sqlldr.bad
OLD_FILE/Jenny_old.log
Hearts_jai.mkv

home/jenny$sed  ‘s/^/rm  -ef  /g’  file_to_remove_list  > file_to_remove_list.sh
home/jenny$chmod  +x   file_to_remove_list.sh
/home/jenny$./ file_to_remove_list.sh
removing Jj12_2.txt
removing Df003.log
removing Awer_cat.lst
removing Sdifre.post
removing Mail34.log
removing TAB/Hourwise_list.doc
removing Sequence.log
removing Created/Sqlldr.bad
removing OLD_FILE/Jenny_old.log
removing Hearts_jai.mkv
/home/jenny$





Example 4)
To remove  files interactively use  -i option.
/home/jenny$ rm    -ir   FILMS
rm: remove  FILMS/ Schindler's List ?y
rm: remove  FILMS/ Chinatown? y
rm: remove  FILMS/ Taxi Driver?y
rm: remove  FILMS/ The Sixth Sense?n
/home/jenny$

y  and n are inputs given by you to remove or  not to  remove resp.




No comments:

Post a Comment