🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

rm Command

Beginner File Management man(1)

Remove files and directories permanently

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
rm [OPTION]... [FILE]...

What Does rm Do?

The rm command removes (deletes) files and directories from the filesystem. Unlike a desktop trash can, rm permanently deletes files — they cannot be easily recovered.

rm is a powerful and potentially dangerous command. The infamous "rm -rf /" command, if run as root, would attempt to delete the entire filesystem. Modern systems have safeguards against this, but caution is still essential.

For directories, rm requires the -r (recursive) flag. The -f flag forces deletion without prompts, while -i provides interactive confirmation for each file.

Options & Flags

OptionDescriptionExample
-r, -R Remove directories and their contents recursively rm -r old-project/
-f Force removal without prompting rm -f temp.log
-i Prompt before every removal rm -i *.txt
-I Prompt once before removing more than 3 files rm -I *.log
-v Verbose - explain what is being done rm -rv old-dir/
-d Remove empty directories rm -d empty-folder/
--preserve-root Do not remove / (root) - enabled by default rm --preserve-root -rf /

Practical Examples

#1 Remove a single file

Deletes the file permanently from the filesystem.
$ rm temp.txt

#2 Remove a directory and all contents

Recursively removes the directory and everything inside it.
$ rm -r old-project/

#3 Force remove without prompts

Forcefully removes the directory without any confirmation prompts.
$ rm -rf /tmp/build-cache/

#4 Interactive removal

Asks for confirmation before deleting each file and subdirectory.
$ rm -ri important-folder/
Output: rm: remove regular file 'important-folder/doc1.txt'? y rm: remove regular file 'important-folder/doc2.txt'? n

#5 Remove files matching pattern

Removes all files with .log extension in the current directory.
$ rm *.log

#6 Remove all except certain files

Removes all files except .txt and .pdf files (requires extglob: shopt -s extglob).
$ rm !(*.txt|*.pdf)

#7 Safe removal with verbose

Shows every file being removed for verification.
$ rm -rv /var/log/old-logs/
Output: removed '/var/log/old-logs/app.log' removed '/var/log/old-logs/error.log' removed directory '/var/log/old-logs/'

Tips & Best Practices

No recovery: Linux has no trash can for rm. Deleted files are gone permanently. Consider using "trash-cli" for a safer alternative.
Safe alias: Add "alias rm='rm -I'" to .bashrc. This prompts when removing more than 3 files, preventing accidental mass deletion.
Never run rm -rf /: This would attempt to delete your entire system. Modern rm has --preserve-root by default, but never test this.
Preview before deleting: Use "ls" with the same pattern before rm: "ls *.log" to verify, then "rm *.log" to delete.

Frequently Asked Questions

How do I recover a deleted file?
Files deleted with rm are very difficult to recover. Tools like extundelete or testdisk may help on ext4, but success is not guaranteed. Prevention (backups) is better than recovery.
What is the difference between rm -r and rmdir?
rmdir only removes empty directories. rm -r removes directories regardless of contents, including all files and subdirectories inside.
How do I delete files older than 30 days?
Use find with rm: find /var/log -name "*.log" -mtime +30 -delete. This is safer than rm with wildcards.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →