PQ
PQ.Hosting

Currency

The rm Command in Linux: Deleting Files, Directories, and Recovering from Mistakes

Author
PQ
March 11, 2026
4 min read
79 views
The rm Command in Linux: Deleting Files, Directories, and Recovering from Mistakes

rm is one of the few Linux commands where a mistake is expensive. No recycle bin, no confirmation by default, no undo. The file disappears immediately. It is worth understanding how it works before needing to delete something important.

Basic Deletion

Delete a file:

rm file.txt

Delete several files at once:

rm file1.txt file2.txt file3.txt

Delete by pattern — all .log files in the current directory:

rm *.log

Delete a file at a specific path:

rm /var/log/nginx/access.log

Deleting Directories: the -r Flag

Without -r, attempting to delete a directory gives is a directory. Recursive deletion with all contents:

rm -r /path/to/directory

Removes everything inside: subdirectories, files, symlinks — no confirmation.

No Prompts at All: the -f Flag

-f (force) suppresses all warnings and produces no error if the file does not exist:

rm -f file.txt

The -rf combination — delete a directory without any questions:

rm -rf /path/to/directory

This is the most dangerous combination in Linux. -rf with a wrong path or a typo means irreversible data loss. Always verify the path before running.

Confirm Before Every Deletion: the -i Flag

The opposite of -f — ask for confirmation on each file:

rm -i *.log

Inconvenient for bulk operations, but saves from accidental deletions when using patterns.

The -I flag (uppercase) asks once if there are more than three files or recursion is used:

rm -rI /var/log/oldlogs/

Show What Is Being Deleted: the -v Flag

rm -rv /tmp/cache/

Useful in scripts where traceability matters — shows exactly what was removed.

Stay Within the Filesystem: --one-file-system

When recursively deleting directories that contain mount points, there is a risk of deleting the contents of a mounted disk or NFS share. The --one-file-system flag stops at filesystem boundaries:

rm -rf --one-file-system /mnt/backup/old/

If there is a mounted partition inside /mnt/backup/old/, rm will skip it.

Files With Unusual Names

A file starting with a dash — rm interprets it as a flag:

rm -- -filename.txt

-- signals that everything after it is an argument, not a flag.

File with spaces in the name:

rm "my file with spaces.txt"

File with unprintable characters — delete by inode:

ls -i
find . -inum 123456 -delete

Delete Everything Except One File

Delete all files in a directory except config.conf:

find /path -not -name "config.conf" -type f -delete

Or via bash extended globbing:

shopt -s extglob
rm !(config.conf)

Safe Alternative: trash-cli

trash-cli sends files to the trash instead of immediate deletion, allowing recovery:

sudo apt install trash-cli

Move to trash:

trash file.txt

List trash contents:

trash-list

Restore a file:

restore-trash

Empty the trash:

trash-empty

If the File Is Already Deleted: Recovery Attempt

rm does not move the file — it removes the directory entry and marks the inode as free. The data on disk remains until overwritten. The sooner recovery starts, the higher the chances.

Recovery tool for ext4:

sudo apt install extundelete
sudo extundelete /dev/sda1 --restore-file path/to/file.txt

On a production server — unmount the partition or set it to read-only first, otherwise new writes will overwrite the deleted data.

Quick Reference

Task Command
Delete a file rm file.txt
Delete multiple files rm file1.txt file2.txt
Delete a directory rm -r /path/dir
No confirmations rm -rf /path/dir
Confirm each file rm -i *.log
Show what is deleted rm -rv /path/dir
Stay within filesystem rm -rf --one-file-system /path/
File with leading dash rm -- -filename.txt
Delete all except one find /path -not -name "keep.txt" -delete
Move to trash instead trash file.txt

Share this article