rm: remove files and directories
rm removes files. There is no recycle bin: once a file is removed, it is gone unless you have a backup. Read this page before you use rm -r.
Common options
| Option | Meaning |
|---|---|
-i | Ask before each removal |
-I | Ask once before removing more than three files or recursing (GNU) |
-r, -R | Remove directories and everything in them |
-f | Force: never ask, ignore missing files |
-v | Show what is removed |
-d | Remove empty directories (like rmdir) |
Examples
rm old.txt # remove a file
rm -i *.bak # ask about each .bak file
rm -r build/ # remove a directory tree
rmdir empty-dir # remove an empty directory (fails if not empty: a safe check)
rm -- -oops.txt # a name starting with "-"
rm ./-oops.txt # the same, another way
rm "file with spaces.txt"
Be careful with rm -rf
A space in the wrong place can be disastrous: rm -rf /tmp/build /* instead of rm -rf /tmp/build/* removes the build directory and then everything on the system. GNU rm refuses to remove / itself, but nothing stops /*.
- Run the same pattern with
lsfirst:ls /tmp/build/*, then replacelswithrm. - In scripts, never write
rm -rf "$DIR/"*without checking that$DIRis set: if it is empty, this becomesrm -rf /*. Userm -rf "${DIR:?}"/*, which stops the script when the variable is empty, andset -u. - Use
rm -Ifor interactive work: it asks once before a big removal. - Do not work as root unless you need to.
Removing files by age, size or name across a tree
Use find, and look at the list before deleting:
find /var/tmp/app -type f -mtime +30 -print # list files older than 30 days
find /var/tmp/app -type f -mtime +30 -delete # then delete them
Removed but the disk is still full?
If a running program still has a file open, the space is not freed until the program closes it. Find such files with lsof +L1 (or lsof | grep deleted) and restart the program. To empty a big log file that a program is writing, truncate it instead of removing it: : > /var/log/app.log or truncate -s 0 /var/log/app.log.
Safer alternatives
On desktops, gio trash file (GNOME) or the trash-cli package moves files to the desktop's trash instead of deleting them. On servers, the real safety net is a backup and file system snapshots (btrfs, ZFS, LVM).