Rm (Unix): Wikis

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.


(Redirected to rm (Unix) article)

From Wikipedia, the free encyclopedia

rm (short for remove) is one of several basic Unix command lines that operates on files and directories. It is used to delete files and directories from a filesystem. The command does not actually destroy data. Rather, the file system is told to forget where the file is located, and the actual disk location may be overwritten by a new file. There are undelete utilities that will attempt to reconstruct the index and can bring the file back if the parts were not reused.

For example, to remove a file named "foo" from a directory one would type:

  % rm -i foo
    remove foo? y

The -i option is used to request a prompt to be sure the file should be removed. This is a safety feature, as files that are removed with rm are more difficult to recover than files that are deleted on a system with a recycle bin.



Common options that rm accepts include:

  • -r, which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in) ("recursive")
  • -i, which asks for every deletion to be confirmed ("interactive")
  • -f, which ignores non-existent files and overrides any confirmation prompts ("force")
  • -v, which shows what is being removed as it happens ("verbose")

rm is often aliased to "rm -i" so as to avoid accidental deletion of files. If a user still wishes to delete a large number of files without confirmation, they can manually cancel out the -i argument by adding the -f option (as the option specified later on the expanded command line "rm -i -f" takes precedence).

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf variant of the command, if run by a superuser on the root directory, would cause the contents of every writable mounted filesystem on the computer to be deleted.

rm is often used in conjunction with xargs to supply a list of files to delete:

xargs rm < filelist

When rm is used on a symbolic link, it deletes the link, but does not affect the target of the link.


Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant. However, GNU rm asks for confirmation if a write-protected file is to be deleted, unless the -f option is used.)

To delete a directory (with rm -r), one must delete all of its contents recursively. This requires that one must have read and write and execute permission to that directory (if it's not empty) and all non-empty subdirectories recursively (if there are any). The read permissions are needed to list the contents of the directory in order to delete them. This sometimes leads to an odd situation where a non-empty directory cannot be deleted because one doesn't have write permission to it and so cannot delete its contents; but if the same directory were empty, one would be able to delete it.

If a file resides in a directory with the sticky bit set, then deleting the file requires one to be the owner of the file.

Protection of /

Sun introduced "rm -rf /" protection in Solaris 10, first released in 2005. Upon executing the command, the system now reports that the removal of / is not allowed.[1] Shortly after, the same functionality was introduced into FreeBSD version of rm utility. GNU rm refuses to execute rm -rf / if the --preserve-root option is given, which has been the default since version 6.4 of GNU Core Utilities was released in 2006.


Systems administrators, designers, and even users often attempt to defend themselves against accidentally deleting files by creating an alias or function along the lines of:

alias rm="rm -i"
rm () { /bin/rm -i "$@" ; }

Unfortunately, this tends to train users to be careless about the wildcards they hand into their rm comamnds, as well as encouraging a tendency to alternately pound y and the return key to affirm removes - until just past the one file they needed to keep. Users have even been seen going as far as "yes | rm files". A compromise that allows users to confirm just once, encourages proper wildcarding, and makes verification of the list easier can be achieved with something like:

if [ -n "$PS1" ] ; then
  rm () 
      ls -FCsd "$@"
      echo 'remove[ny]? ' | tr -d '\012' ; read
      if [ "_$REPLY" = "_y" ]; then
          /bin/rm -rf "$@"
          echo '(cancelled)'

It's important to note that this function should not be made into a shell script, which would run a risk of it being found ahead of the system rm in the search path, nor should it be allowed in non-interactive shells where it could break batch jobs. Enclosing the definition in the if [ -n "$PS1" ] ; then ....  ; fi construct protects against the latter.

See also


External links

Got something to say? Make a comment.
Your name
Your email address