Linux rm command

Updated: 11/30/2020 by Computer Hope
rm command

On Unix-like operating systems, the rm command removes (deletes) files.

This page covers the GNU/Linux version of rm.

Description

rm removes each file specified on the command line. By default, it does not remove directories.

When rm is executed with the -r or -R options, it recursively deletes any matching directories, their subdirectories, and all files they contain. See removing directories below for details.

The removal process unlinks a file name in a filesystem from its associated data, and marks that space on the storage device as usable by future writes. In other words, when you remove a file, the data in the file isn't changed, but it's no longer associated with a file name.

The data itself is not destroyed, but after being unlinked with rm, it becomes inaccessible. Remove your files wisely! It's not like putting something in the Windows Recycle Bin; once you rm a file or directory, there is no way to undo it.

Note

If you want is to completely wipe the data on the disk, use the shred command instead. shred will overwrite the file's contents so that they cannot be reconstructed later.

Syntax

rm [-f | --force] {[-i | --interactive[=always]] | [-I | --interactive=once] |
   [--interactive=never]} [--one-file-system] [--no-preserve-root |
   --preserve-root] [-r | -R | --recursive] [-d | --dir] [-v | --verbose] 
   FILE...
rm --help
rm --version

Options

-f, --force Ignore nonexistant files, and never prompt before removing.
-i Prompt before every removal.
-I Prompt once before removing more than three files, or when removing recursively. This option is less intrusive than -i, but still gives protection against most mistakes.
--interactive[=WHEN] Prompt according to WHEN: never, once (-I), or always (-i). If WHEN is not specified, then prompt always.
--one-file-system When removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument
--no-preserve-root Do not treat "/" (the root directory) in any special way.
--preserve-root Do not remove "/" (the root directory), which is the default behavior.
-r, -R, --recursive Remove directories and their contents recursively.
-d, --dir Remove empty directories. This option permits you to remove a directory without specifying -r/-R/--recursive, provided that the directory is empty. In other words, rm -d is equivalent to using rmdir.
-v, --verbose Verbose mode; explain at all times what is being done.
--help Display a help message, and exit.
--version Display version information, and exit.

Usage notes

If the -I/--interactive=once option is given, and there are more than three files or the -r/-R/--recursive options are specified, rm prompts before deleting anything. If the user does not respond yes/y/Y to the prompt, the entire command is aborted.

If a file is unwritable, stdin is a terminal, and the -f/--force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. If the response is not yes/y/Y, the file is skipped.

Removing directories

By default, rm does not remove directories. If the -r/-R/--recursive option is specified, however, rm removes any matching directories and their contents.

If the specified directory is empty, it may be removed with the -d/--dir option, instead.

File names starting with a dash

To remove a file whose name begins with a dash ("-"), you can specify a double dash ("--") separately before the file name. This extra dash is necessary so that rm does not misinterpret the file name as an option.

For instance, if there is a file in your current directory named "-file.txt", you can delete it with the command

rm -- -file.txt

Or, you can delete it by referring to it with a pathname. For instance, if the file "-file.txt" was located in the directory "/home/hope", you could delete it using:

rm /home/hope/-file.txt

...or, if /home/hope is currently your working directory,

rm ./-file.txt

...will work as well.

The unlink command is essentially the same as rm, but it's defined very strictly by the POSIX standard. It operates on only one file at a time; it does not operate on directories; its behavior is not modified by any command line options. It does one thing and one thing only: calls the unlink() system call on a single file.

The version of rm used by most versions of Linux (GNU rm) has all the options and niceties listed above: safety checks, interactive prompting, conditional deletion, recursive operation. It is similar to unlink in that it makes the unlink() system call, but it may also call unlinkat() if a specified pathname is relative rather than absolute.

In other words, rm is a much friendlier way to use unlink.

Examples

rm myfile.txt

Remove the file myfile.txt. If the file is write-protected, you are prompted to confirm you want it deleted.

rm -f myfile.txt

Remove the file myfile.txt. You will not be prompted, even if the file is write-protected; if rm can delete the file, it will.

rm *

Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it.

rm -f *

Remove all files in the working directory. rm will not prompt you for any reason before deleting them.

rm -i *

Attempt to remove every file in the working directory, but prompt before each file to confirm.

rm -I *

Remove every file in the working directory; prompt for confirmation if more than three files are being deleted.

rm -r mydirectory

Remove the directory mydirectory, and any files and directories it contains. If a file or directory that rm tries to delete is write-protected, you are prompted to make sure you want it deleted.

rm -rf mydirectory

Same as the above command, but you are never prompted; if rm can delete the files, it will.

rmdir — Remove a directory.
shred — Overwrite a file's contents, irrevocably destroying them.