Rm

From LQWiki
Jump to navigation Jump to search

rm is the unix command to remove files or directories. Rm removes each specified file. By default, it does not remove directories. If a file is unwritable, the standard input is a tty, and the -f or --force option is not given, rm prompts the user for whether to remove the file. If the response does not begin with `y' or `Y', the file is skipped.

Syntax

rm [OPTION] file   

Options

  • -d, --directory - unlink file, even if it is a non-empty directory (super-user only)
  • -f, --force - ignore nonexistent files, never prompt
  • -i, --interactive - prompt before any removal
  • -r, -R, --recursive - remove the contents of directories recursively
  • -v, --verbose - explain what is being done
  • --help - display this help and exit
  • --version - output version information and exit

Examples

To remove file1.txt:

$ rm file1.txt

To remove the directory 'testing', including all files it contained:

$ rm -r testing

To remove all files starting with the letters 'file' (eg file1.bin, filed.txt).

$ rm file*

To have rm ask you for confirmation before deleting file1.txt.

$ rm -i file1.txt

To remove a file whose name starts with a `-', for example `-foobar', use one of these commands:

$ rm ./-foobar 
$ rm -- -foobar 

Note that if you use rm to remove a file, it is usually possible for others to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred.

Warnings

rm is a dangerous command (especially with wild cards) and should be treated with a high degree of respect. So here are some warnings:

  • Don't Use rm as Root. Unless you absolutely have to. As with everything with linux, avoid using rm under root if you can possibly avoid it. This will help to prevent deleting vital system files, which should be protected by permissions. Deleting vital system files is not fun to fix and very unhelpful.
  • Check for Typos! Especially for spaces.
  • Be Wary of Wildcards! Especially if you are in a vital directory (e.g. /usr/bin).
I (Geniarse) once typed the command the command rm foo * in /usr/bin (as root of course). Note the space between * and foo. This is a very bad typo to do as this deletes the file foo and then all files in directory, rather than all files beginning with foo as intended. SO CHECK FOR TYPING MISTAKES!
  • Be VERY Careful Using the -rf Flags. Some people advise that if you do use the -rf (recursive and forcible) flags, that you use them at the end of the command. This prevents the following: rm -rf /<your pet kitten presses the enter key>
  • Don't use Wildcards to Recursively Remove Files under a Hidden Directory! Ever wanted to remove all files recursively under a hidden directory (name beginning with "." e.g. ".foo") entering command rm -R .*/*? Well, a hint is DON'T. As the "name" for parent directory is listed as "..", the parent directory will be included, hence all files and directories in parent directory will be included, not good.

See Also

External links