Delete Files in Linux Terminal: 9 Safe Ways, 5 Mistakes
To delete files in the Linux terminal you use rm, and for directories rmdir or rm -r. That part takes one line. The useful part is knowing what these commands do in the awkward cases: a file you do not own, a name that starts with a dash, an empty variable in a script, or a deleted file whose space never comes back. I tested all of that on 22 November 2026 in a Debian 13 container (GNU coreutils 9.7, trash-cli 0.24.5.26, secure-delete 3.1), as root and as a normal user, and I corrected two claims from the earlier version of this post along the way.
Contents
- Delete files and directories: the basics
- Ask before you delete files: -i, -I and -v
- Awkward file names
- Who may delete a file: the directory decides
- Delete files with find, and the -delete order trap
- rm -rf safety rails (and their limits)
- A recycle bin for the terminal: trash-cli
- Secure deletion: shred and srm
- Deleted, but the disk is still full
Delete files and directories: the basics
user$ touch a.txt b.txt; mkdir -p empty full/sub; touch full/sub/f; rm a.txt; ls
b.txt
empty
full
user$ rmdir full
rmdir: failed to remove 'full': Directory not empty
user$ rm full
rm: cannot remove 'full': Is a directory
user$ rmdir empty && rm -r full && ls
b.txt
rmdir only removes empty directories, which makes it the safe choice when you expect a directory to be empty: if it is not, you find out instead of losing data. rm -r removes a directory and everything below it. The full option list is in the GNU rm manual.
Ask before you delete files: -i, -I and -v
Interactive mode is the cheapest insurance when you delete files by hand. -i asks for every file. I answered y, n, y and only x2 survived. -I asks once when you delete more than three files (or recursively), which is a good default alias because it does not nag on single files:
user$ printf "n\n" | rm -I y1 y2 y3 y4; ls y*
rm: remove 4 arguments? y1
y2
y3
y4
user$ rm -v y1 y2
removed 'y1'
removed 'y2'
Awkward file names
Odd names are where people fail to delete files, or delete the wrong ones. A file named -rf cannot be removed with rm -rf, because rm reads that as options (and, with no file named, does nothing). End the options with --. Quote names with spaces or glob characters:
user$ rm -- -rf; rm "my file.txt"; rm "*"; ls
b.txt
d
x2
y3
y4
The quotes around * matter more than anything else on this page. Without them the shell expands the asterisk and you delete every file in the directory.
Who may delete a file: the directory decides
Who may delete files is decided in a way that surprises many people. I put a root-owned file with mode 600 into a normal user's directory. The user could not read it, but deleted it without any error, and without a question, because the input was not a terminal:
-rw-------. 1 root root 7 Nov 22 03:36 rootfile
user$ rm rootfile
[rc=0]
The reverse case: the user owns labfile, but it sits in a root-owned 755 directory:
user$ rm /srv/shared/labfile
rm: cannot remove '/srv/shared/labfile': Permission denied
Deleting a file is a change to the directory, so write permission on the directory is what counts, not ownership of the file. Shared directories such as /tmp use the sticky bit to stop users from deleting each other's files.
Delete files with find, and the -delete order trap
To delete files by age or pattern, use find. Always run the expression with -print first, then swap in -delete:
user$ find logs -name "*.log" -mtime +30 -print
logs/app1.log
logs/app2.log
logs/app3.log
user$ find logs -name "*.log" -mtime +30 -delete; ls -R logs
logs:
old
today.log
logs/old:
keep.txt
Only the three old logs went. Now the same idea with -delete written first:
user$ find logs -delete -name "*.txt"; ls logs
ls: cannot access 'logs': No such file or directory
find evaluates from left to right, so -delete ran on every file before the name test was ever checked. The whole tree was gone. Put -delete last, always.
rm -rf safety rails (and their limits)
The old version of this post said rm -rf / "will recursively delete everything". Current coreutils refuses:
$ rm -rf /
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe
That rail only protects / itself. The classic script accident is an empty variable, and nothing stops it:
$ touch /root/canary; D=""; rm -rf "$D/root/canary"; ls /root/canary
ls: cannot access '/root/canary': No such file or directory
With D empty, "$D/root/canary" became the absolute path /root/canary. In scripts, let the shell stop you:
$ set -u; unset D3; rm -rf "${D3}/tmp/x"
bash: line 1: D3: unbound variable
$ rm -rf "${D4:?is empty}/tmp/x"
bash: line 1: D4: is empty
set -u catches unset variables, and ${VAR:?message} also catches empty ones. Use the second form in every rm -rf line that contains a variable.
A recycle bin for the terminal: trash-cli
rm has no undo. Scripts that delete files should keep using rm, but at an interactive prompt trash-cli moves files to the same trash folder the desktop uses:
user$ echo keep > notes.txt; trash-put notes.txt; ls notes.txt; trash-list
ls: cannot access 'notes.txt': No such file or directory
2026-11-22 03:36:48 /home/lab/t/notes.txt
user$ trash-restore notes.txt <<< 0; cat notes.txt
keep
Secure deletion: shred and srm
$ shred --help | grep -E "iterations|-z,"
-n, --iterations=N overwrite N times instead of the default (3)
-z, --zero add a final overwrite with zeros to hide shredding
$ shred -v -u /tmp/s.bin
shred: /tmp/s.bin: pass 1/3 (random)...
shred: /tmp/s.bin: pass 2/3 (random)...
shred: /tmp/s.bin: pass 3/3 (random)...
shred: /tmp/s.bin: removing
shred: /tmp/s.bin: renamed to /tmp/00000
srm from the secure-delete package took 3.8 seconds for a 1 MB file in the same container. Neither works on directories (shred: /tmp/sd: failed to open for writing: Is a directory), and overwriting in place has limits on SSDs, copy-on-write and journaling file systems; the post on shred and dd covers that in detail. For sensitive data the reliable answer is full-disk encryption from day one.
Deleted, but the disk is still full
$ head -c 15M /dev/zero > /mnt/small/big; (sleep 30 < /mnt/small/big &); rm /mnt/small/big; df -h /mnt/small
tmpfs 20M 15M 5.0M 75% /mnt/small
lr-x------. 1 root root 64 Nov 22 03:36 0 -> /mnt/small/big (deleted)
When you delete files that a process still has open, the name disappears but the space stays used until the process closes it. ls -l /proc/*/fd | grep deleted (or lsof +L1) finds the culprit; restart that service to free the space. This is the usual reason a log cleanup "did nothing". More everyday commands are in the Linux survival guide.
Comments
Post a Comment