Linux rm command

Updated: 01/24/2018 by Computer Hope

About rm

The rm ("remove") command is used to delete files. When used recursively, it may be used to delete directories.


rm removes each file specified on the command line. By default, it does not remove directories; 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 filename.

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.


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] 
rm --help
rm --version


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.
Remove directories and their contents recursively.
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.
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 will prompt 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 will remove 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 essentially does the same thing as rm, but it is 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.


rm myfile.txt

Remove the file myfile.txt. If the file is write-protected, you will be prompted to confirm that you really want to delete it.

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 will be prompted to make sure that you really want to delete it.

rm -rf mydirectory

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

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