How do I use rm safely on a server?

List what a command will remove before you run it, with ls, find or echo. Use rm -I so that rm asks once before a recursive delete, and write variables as "${dir:?}" so that an empty one stops the command. The built-in --preserve-root protects only the path /.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

rm has no trash folder and no undo. Before you delete, make the command show you what it will touch: list the files with ls or find, or put echo in front of the command. Then use rm -I so that rm asks once before a recursive delete, and quote variables so that an empty one cannot widen the command into something huge.

Look before you delete

The server has four releases of an application. You want to remove the old ones. Start with a list, not with rm:

$ ls /srv/app/releases
20261002  20261020  20261114  20261122
$ find /srv/app/releases -mindepth 1 -maxdepth 1 -mtime +14 | sort
/srv/app/releases/20261002
/srv/app/releases/20261020

find shows the two folders that were last changed more than 14 days ago. If you plan to delete with a pattern, let the shell show what the pattern expands to. echo prints the command instead of running it:

$ echo rm -r /srv/app/releases/202610*
rm -r /srv/app/releases/20261002 /srv/app/releases/20261020

The two lines name the same folders. Now the real command has a list you have checked, and the list is the thing you type.

Make rm ask

rm -i asks before every file. rm -I asks once, before it removes more than three files or removes anything recursively. The rm manual calls it less intrusive and says it still protects against most mistakes. Here the answer is n:

$ printf 'n\n' | rm -rI /srv/app/releases/202610*
rm: remove 2 arguments recursively?
$ ls /srv/app/releases
20261002  20261020  20261114  20261122

Nothing was removed. The question names the number of arguments, which is the check you want: if you expected two and it says 40, answer no. On your keyboard you type y or n and Enter. Here printf types the answer for you.

An alias such as alias rm='rm -I' in ~/.bashrc helps only in your own interactive shell. Scripts, cron jobs and sudo rm do not use it. Use it as a habit, not as a guarantee.

When you are sure, remove the two folders by name. -v prints what goes:

$ rm -rv /srv/app/releases/20261002 /srv/app/releases/20261020
removed '/srv/app/releases/20261002/app.js'
removed '/srv/app/releases/20261002/README'
removed directory '/srv/app/releases/20261002'
removed '/srv/app/releases/20261020/app.js'
removed '/srv/app/releases/20261020/README'
removed directory '/srv/app/releases/20261020'

For a single file, -i is fine:

$ printf 'n\n' | rm -i /srv/app/logs/app-today.log
rm: remove regular file '/srv/app/logs/app-today.log'?

What --preserve-root does

GNU rm refuses to remove / itself. It is the default, and the manual says so:

$ rm -rf /
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe

That is all it does. It looks at the exact path / and nothing else. /home, /var and /srv/app get no protection, and /* is not / either. The shell turns /* into a list before rm starts, and rm sees /bin, /etc and the others as ordinary folders. Never add --no-preserve-root.

Quote variables and stop on empty ones

The classic accident is a variable that is empty. Say a script has rm -rf $dir/* and dir is empty. The shell sees this:

$ dir=; echo rm -rf $dir/* | cut -c1-33
rm -rf /bin /boot /dev /etc /home

I used echo so that nothing is deleted, and cut to show only the first five names. The command is rm -rf /* in disguise, and --preserve-root does not help. Write the variable as "${dir:?}". With :?, bash stops with an error when the variable is unset or empty:

$ bash -c 'dir=; rm -rf "${dir:?}"/*'
bash: line 1: dir: parameter null or not set

bash -c stands in for a script here. set -u at the top of a script is a second net. It stops on a variable that was never set:

$ bash -c 'set -u; rm -rf "$dir"/*'
bash: line 1: dir: unbound variable

But it does not stop on one that is set and empty:

$ bash -c 'set -u; dir=; echo "still runs: rm -rf $dir/*"'
still runs: rm -rf /*

So use ${dir:?} where it matters. The quotes also keep a path with a space in one piece. Without them, /srv/old backups is two arguments.

Delete by age with find, in the right order

find reads its expression from left to right. An action that comes before a test acts on everything. The find manual warns that putting -delete first makes find try to delete everything below the starting points. This command uses -print in the wrong place, so you can see the effect without losing anything:

$ find /srv/app/logs -print -name '*.log' -mtime +30 | sort
/srv/app/logs
/srv/app/logs/app-2026-08.log
/srv/app/logs/app-2026-09.log
/srv/app/logs/app-2026-10.log
/srv/app/logs/app-today.log

It listed today’s log and the folder itself. The right order has the tests first and the action last. -type f keeps a folder whose name ends in .log out of the list. Run it with no action first:

$ find /srv/app/logs -type f -name '*.log' -mtime +30 | sort
/srv/app/logs/app-2026-08.log
/srv/app/logs/app-2026-09.log
/srv/app/logs/app-2026-10.log

When the list is what you want, add -delete at the end and nothing else:

$ find /srv/app/logs -type f -name '*.log' -mtime +30 -delete
$ ls /srv/app/logs
app-today.log

Mistakes people make

You run rm -rf when rm -r would do. The -f silences errors and questions, which are the two things that save you.

You type a relative path in the wrong directory. Use the full path, or check with pwd first.

You work as root out of habit. As a normal user a wrong command fails on files you do not own. Read sudo for one command for a way to give root only where it is needed.

You rely on a backup you never tested. A tested backup is the only undo.

This article covers what a person who is already logged in can destroy. Who gets in is a separate question; see SSH keys and turning off password login.

Valakes reads the command you type before it runs. An rm -rf opens a High risk card with the reason and a count of what it would delete, and on a production host you type the host name before it goes. It reads only the line you type, so a script is outside it, and a path that depends on a variable, such as "$HOME/x", still asks but gets no count. Guide: How do I stop a destructive command on a production server before it runs?

Questions

Can I get my files back after rm?

Not from rm. It removes the file name for good, and a server has no trash folder. Restore from a backup or a snapshot. Undelete tools exist for some file systems, but they are unreliable, and every write to the disk lowers the chance.

Does rm -I protect me from rm *?

Yes when the pattern matches more than three files. rm -I asks once before it removes more than three files or removes anything recursively. For one to three plain files it does not ask.

Does --preserve-root make rm -rf safe?

No. It stops rm from removing the single path / and nothing else. rm -rf /* is expanded by the shell into /bin /etc /home and the rest, and rm sees each of them as an ordinary folder.

Should I use find -delete or rm?

find -delete is good for files that match a test, such as logs older than 30 days. It removes files and empty directories only. To remove a folder with content, use rm -r on a path you have listed first.

References

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.