rm


The rm command removes one or more files from a directory and can also be used to remove the directory itself. Going back to our earlier discussion on permissions, you must have both write and execute permissions on a directory in order to remove a file from it. If you own the directory from which you are going to remove files, then you can probably remove files from it. If, however, you don't have the appropriate permissions on a directory, then the rm fails.

As with some of the other commands we have covered, you can use the -i option, which asks you to confirm each file as it is removed. This means that if you are asked whether you really wish to remove a file and you respond n , then the file is not removed. If you respond y , the file is removed.

You can also use the -r (or -R ) option to recursively delete the contents of directories and then delete the directories. This means you can recursively delete the files and directories specified. If there is any question in your mind about whether or not you wish to recursively delete files and directories, then use the -i option along with -r .

You can use the -f option to remove files and directories, which performs removes without asking you to confirm them.

The following example performs a long listing of the directory krsort.dir.new , interactively prompts the user to see whether he or she wants to delete the files in this directory, and then lists the contents of this directory again, showing that all files have not been deleted because the user responded n :

 $  ls -l krsort.dir.new  total 168 -rwxr-xr-x   1 denise   users    34592 Oct 27 18:44 krsort -rwxr-xr-x   1 denise   users    3234 Oct 27 18:46 krsort.c -rwxr-xr-x   1 denise   users   32756 Oct 27 18:46 krsort.dos -rw-r--r--   1 denise   users    9922 Oct 27 18:46 krsort.q -rwxr-xr-x   1 denise   users    3085 Oct 27 18:46 krsortorig.c $  rm -i krsort.dir.new/*  ../krsort.dir.new/krsort: ? (y/n)  n  ../krsort.dir.new/krsort.c: ? (y/n)  n  ../krsort.dir.new/krsort.dos: ? (y/n)  n  ../krsort.dir.new/krsort.q: ? (y/n)  n  ../krsort.dir.new/krsortorig.c: ? (y/n)  n  $  ls -l krsort.dir.new  total 168 -rwxr-xr-x   1 denise   users    34592 Oct 27 18:44 krsort -rwxr-xr-x   1 denise   users     3234 Oct 27 18:46 krsort.c -rwxr-xr-x   1 denise   users    32756 Oct 27 18:46 krsort.dos -rw-r--r--   1 denise   users     9922 Oct 27 18:46 krsort.q -rwxr-xr-x   1 denise   users     3085 Oct 27 18:46 krsortorig.c $ 

Note that the response to being asked whether the file should be deleted was n in all cases. This means that none of the files have been removed. A y response to any question results in that file being removed from the directory. To interactively delete a directory, you combine the options -i and -r of the rm command. If, however, you do not delete every file in a directory, then the directory is not removed if the -i option is used. The first part of the following example shows all but the file krsort being removed from the directory krsort.dir.new . The directory is not deleted because this file still exists. When the file is removed in the second part of this example, the directory itself is then deleted:

 $  rm -ir krsort.dir.new  directory krsort.dir.new: ? (y/n)  y  krsort.dir.new/krsort: ? (y/n)  n  krsort.dir.new/krsort.c: ? (y/n)  y  krsort.dir.new/krsort.dos: ? (y/n)  y  krsort.dir.new/krsort.q: ? (y/n)  y  krsort.dir.new/krsortorig.c: ? (y/n)  y  krsort.dir.new: ? (y/n)  y  rm: directory krsort.dir.new not removed.  Directory not empty $  rm -ir krsort.dir.new  directory krsort.dir.new: ? (y/n)  y  krsort.dir.new/krsort: ? (y/n)  y  krsort.dir.new: ? (y/n)  y  $ 

rm - Remove files and directories.

Options

 

-i

Interactive remove whereby you are prompted to confirm that you wish to remove an existing file.

 

-f

Force files to be removed.

 

-r (-R)

Recursively remove the contents of the directory and then the directory itself.



HP-UX 11i Systems Administration Handbook and Toolkit
HP-UX 11i Systems Administration Handbook and Toolkit (2nd Edition)
ISBN: 0131018833
EAN: 2147483647
Year: 2003
Pages: 301

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net