LinuxCommandLibrary

rm

Remove files

TLDR

Remove specific files

$ rm [path/to/file1 path/to/file2 ...]
copy

Remove specific files ignoring nonexistent ones
$ rm [[-f|--force]] [path/to/file1 path/to/file2 ...]
copy

Remove specific files interactively prompting before each removal
$ rm [[-i|--interactive]] [path/to/file1 path/to/file2 ...]
copy

Remove specific files printing info about each removal
$ rm [[-v|--verbose]] [path/to/file1 path/to/file2 ...]
copy

Remove specific files and directories recursively
$ rm [[-r|--recursive]] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Remove empty directories (this is considered the safe method)
$ rm [[-d|--dir]] [path/to/directory]
copy

SYNOPSIS

rm [OPTION]... FILE...
rm [OPTION]... DIRECTORY...

PARAMETERS

-f, --force
    Ignore nonexistent files and arguments, never prompt. This option overrides -i and -I.

-i
    Prompt before every removal. This provides a safety net, asking for confirmation for each file or directory.

-I
    Prompt once before removing more than three files, or when removing recursively. This offers a less intrusive safety prompt than -i.

-r, -R, --recursive
    Remove directories and their contents recursively. Essential for deleting non-empty directories.

-v, --verbose
    Explain what is being done. Prints the name of each file as it is processed.

--no-preserve-root
    Do not treat '/' specially. This option is extremely dangerous as it allows rm -rf / to delete the entire root filesystem, making the system unbootable. It's normally protected by default.

-d, --dir
    Remove empty directories. Can be used as an alternative to rmdir for empty directories.

DESCRIPTION

The rm command in Linux is used to remove (delete) files and directories. Unlike graphical user interfaces, rm typically does not move files to a 'trash' or 'recycle bin'; once deleted, they are gone permanently and often unrecoverable without specialized data recovery tools.

By default, rm will remove specified files. If a file is write-protected, it may prompt for confirmation before deletion. To remove directories, the -r (or --recursive) option must be used, which deletes the directory and all its contents recursively. The -f (or --force) option can be used to override prompts and ignore non-existent files, making it a powerful and potentially dangerous tool.

Due to its destructive nature, users should exercise extreme caution when using rm, especially with wildcards or the -r and -f options together, as a single command can irrevocably delete large amounts of data.

CAVEATS

Irreversible Operation: Data deleted with rm is typically not moved to a trash bin and is extremely difficult, if not impossible, to recover, especially on modern filesystems (e.g., SSDs where TRIM is active).

Permissions: To delete a file, you need write permission on the parent directory of the file, not necessarily on the file itself. However, rm may still prompt if the file itself is write-protected (unless -f is used).

Wildcard Caution: Using wildcards (e.g., `*`) with rm can be very dangerous. A common mistake is `rm -rf *.bak` which is fine, but `rm -rf * .bak` (note the space) could delete everything in the current directory if you're not careful.

Root Directory: The `rm -rf /` command is notoriously dangerous. GNU rm protects against this by default, requiring the `--no-preserve-root` option to proceed. Never use this unless you fully understand the implications and have backups.

SAFETY ALIASES

Many users create an alias for rm in their shell configuration (e.g., `~/.bashrc`) to add a layer of protection. Common aliases include `alias rm='rm -i'` to always prompt for confirmation, or `alias rm='trash'` if using a tool like `trash-cli` that moves files to a trash bin instead of deleting them immediately.

DELETING FILES WITH SPECIAL CHARACTERS

Files with names starting with a hyphen (e.g., `-my-file.txt`) can be problematic because rm might interpret them as options. To delete such files, use `rm -- -my-file.txt` (the `--` signifies the end of options) or `rm ./-my-file.txt` (using a relative path).

HISTORY

The rm command has been a fundamental utility in Unix-like operating systems since their early days. It's a direct wrapper around the unlink(2) system call, which removes a hard link to a file. Over time, options like -r for recursive deletion and -i for interactive prompting were added to enhance its functionality and safety. The GNU Coreutils version, widely used in Linux distributions, introduced the crucial `--no-preserve-root` safeguard in response to accidental root directory deletions. Its design reflects the Unix philosophy of providing powerful, focused tools, relying on user caution for their safe operation.

SEE ALSO

rmdir(1), mv(1), unlink(2), shred(1), trash-cli

Copied to clipboard