$_ CLI In Depth
Learning progress 0%
lesson files globbing find

Deleting Safely: rm, rmdir & Why There's No Undo

5 min read Module 4: File Manipulation, Globbing & find Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:09

What rm actually does

The command rm calls the unlink system call for each name it is given. Unlink removes one directory entry and decrements the link count of the inode. When the count reaches zero and no process holds the file open, the kernel frees the blocks and the inode. Until then, the data may still be readable through another name or through an open descriptor, which is the reason a running process can keep writing to a log file that has been removed.

Freed blocks are reused by the filesystem, and the old bytes may remain on the disk until they are overwritten. That is why a deleted file is not reliably recoverable with ordinary tools, and why shredding a file does not guarantee erasure on journaled filesystems or on solid state drives. The right defense is to avoid the deletion or to have a backup, not to count on recovery.

BASH5 lines
cd /workspace
mkdir -p trash/full
touch trash/a.txt
rm trash/a.txt
ls trash

The file is gone from the listing at once. The space is freed as soon as the link count reaches zero, which here is immediately.

rmdir removes only empty directories

The command rmdir removes a directory, but only when the directory is empty. It refuses to remove a directory that holds any entry, including hidden ones, and it reports "Directory not empty". This is a useful safety net. A script that uses rmdir instead of rm -r cannot delete a tree that contains files it does not know about. Use rmdir in the common case of removing a directory you created and expect to be empty.

rm -r and the options that protect you

The option -r removes a directory and everything under it. It is the most dangerous option in common use, because one wrong path deletes a tree. The option -i prompts before each removal, and -I prompts once when more than three files are removed or when the operation is recursive. The option -f suppresses prompts and errors for missing files, which is useful in scripts that must succeed even when a file is absent, but it also hides mistakes.

The option -v prints each name as it is removed. In a new directory, run the command with -v first to see what it would do, or use find to list the same names. A list you have read is far safer than a command you have only imagined.

BASH3 lines
cd /workspace
touch -- -start-with-dash.txt
rm -v -- -start-with-dash.txt

The double dash ends option parsing, so the name is treated as a file. Without it, rm would read the name as an option and fail.

Protecting the root and other important paths

GNU rm refuses to recurse into the root directory by default, which is a guard against a command like rm -rf / with a stray space. Keep that protection on, and be wary of variables that may expand to nothing. A command such as rm -rf "$dir/"* deletes everything in the current directory if $dir is empty, because the path becomes the root. Quoting the variable does not help when the variable is empty. Check the variable first, and use the shell option set -u to make an unset variable an error.

Warning

Do not run rm with a glob and no check. A pattern such as rm * in the wrong directory removes every file there, and a hidden file is only removed if you name it. Run ls with the same pattern first, and read the result.

A safer habit: move before you remove

A practical alternative is to move the files to a holding directory first, and to delete that directory after a delay. A move inside one filesystem is only a rename, so it is as fast as a delete and fully reversible until you empty the holding area. Many desktop trash systems do exactly this. On a server, the same pattern is a cheap insurance policy.

Deleting in a loop

A common script removes files older than a given age. It is tempting to write a loop over ls output, but a safer structure lets find select the files and performs the deletion with -delete or with -exec rm plus a batch. Use find -mtime with a clear limit, run the find without the action first to see the list, and only then add the action. A dry run with -print costs nothing and catches the mistake you did not expect.

Deleting a file that is open in another program does not stop that program. The program keeps writing to the unlinked inode until it closes the file, and the space appears only after it does. A tool that expects a file to disappear immediately can be confused by this, so restart the program or truncate the file if the space matters more than the program state.

Recovering what was lost

If the data matters, the defense is a copy made before the deletion, not a recovery tool after it. Snapshots on a filesystem that supports them, regular backups to another disk, and version control for text files are all reliable. Forensic recovery tools can sometimes find the bytes of a deleted file, but they cannot promise the whole file or its name, and they do not work on every storage device. Plan so that you never need them.

Practice in the lab

The lab removes an empty directory, shows that rmdir refuses a directory with contents, and removes a file in verbose mode. Each result is saved, so the checker can confirm both what was removed and what was refused.

How the kernel decides who may access a file

Visualizer

The kernel picks one class (owner, group or other) and uses only that class’s bits. Directories read r, w and x differently, and the sticky bit limits who can delete.

1 / 4
-type
-owner
-
-
rgroup
w
x
-other
-
-
process
uid 1000, gid 1000
file
owner 1000, group 1000
mode
070

The kernel knows the process’s uid and gid, and the file’s owner and group. It picks exactly one class for the check and tries them in order: owner, group, other.

cd "$(mktemp -d /tmp/perm.XXXX)" && touch own.txt && chmod 070 own.txt && cat own.txt; echo "status $?"; stat -c "%a %A" own.txt
Terminal lab · 3 steps · real Linux in your browser

Remove safely

Use rmdir on an empty directory, see its refusal on a full one, and remove a file in verbose mode.

01
Remove an empty directory
Remove the empty directory trash/empty with rmdir: ``bash rmdir trash/empty ``
Show hint for step 1
rmdir succeeds only when the directory has no entries.
02
See rmdir refuse a full directory
Try rmdir on trash/full and save its error message: ``bash rmdir trash/full 2> rmdir-error.txt ``
Show hint for step 2
The error goes to standard error, so redirect that stream.
03
Remove a file in verbose mode
Remove trash/a.txt and save the message rm prints: ``bash rm -v trash/a.txt > removed.txt ``
Show hint for step 3
-v prints one line for each name it removes.
Check yourself Question 1 of 4
Concept Check Removing only empty directories recall

Which command removes a directory only when it is empty?

Recap & Key Takeaways

rm removes a name, and the kernel frees the data only when the link count is zero and no process has the file open.
rmdir removes only empty directories, which makes it a useful guard in scripts.
Use rm -v to see each removal, and -- before names that start with a dash.
An unset variable in a path can turn rm -rf into a command that deletes far more than intended, so check variables first.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue