rm Linux Command
Removes (deletes) files or directories. Use -rf for recursive force delete.
What does rm do?
Removes (deletes) files or directories. Use -rf for recursive force delete.
Syntax
rm [options] filePractical example
rm -rf /tmp/old-folderQuestions about rm
What does rm -rf do?
The `-r` flag deletes directories recursively (including all contents), and `-f` forces deletion without prompting. Use with extreme caution as deleted files cannot be recovered.
How do I delete a file safely with rm?
Use `rm -i filename` to be prompted before each deletion. This prevents accidental removal of important files.
How do I delete all files in a directory?
Use `rm -r directory/` to delete a directory and its contents, or `rm *` to delete all files in the current directory (not directories).
How do I delete files matching a pattern?
Use glob patterns: `rm *.log` deletes all .log files. Use `rm -f *.log` to suppress warnings about missing files.
Can I recover files deleted with rm?
No, `rm` permanently deletes files without sending them to a trash bin. There is no built-in recovery mechanism. Always back up important data.
How do I delete a file that starts with a dash?
Use `--` before the filename: `rm -- -filename` to prevent the filename from being interpreted as an option.
What is the difference between rm and rmdir?
`rm` deletes files and directories (with `-r`). `rmdir` only removes empty directories, making it safer for removing empty folders.
Related File Management
mvMoves or renames files and directories.
rmdirRemoves an empty directory. Safer than rm -r for removing directories.
cpCopies files or directories from one location to another.
catDisplays the entire contents of a file on the terminal.
touchCreates an empty file or updates the timestamp of an existing file.
lessViews file contents page by page with forward and backward navigation.