# How do I use rm safely on a server?

How to delete files on a server safely. What rm --preserve-root covers and misses, how rm -I asks, how to quote variables and how to check with find first.

Source: https://valakes.com/learn/safe-rm-on-servers/

Last updated: 2026-11-27

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 /.

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

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`:

```console
$ 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:

```console
$ 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`:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/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](https://valakes.com/learn/ssh-keys/) and [turning off password login](https://valakes.com/learn/disable-ssh-password-login/).

## In Valakes

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?](https://valakes.com/guides/stop-rm-rf-on-production/)

[Read the docs: Command check](https://docs.valakes.com/command-check/)

[Help centre: A command was held](https://support.valakes.com/a-command-was-held/)

## 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

- [rm(1) on Ubuntu 24.04, -i, -I and --preserve-root](https://manpages.ubuntu.com/manpages/noble/man1/rm.1.html)
- [rm(1) at man7.org, -i, -I and --preserve-root](https://man7.org/linux/man-pages/man1/rm.1.html)
- [find(1) on Ubuntu 24.04, the -delete action](https://manpages.ubuntu.com/manpages/noble/man1/find.1.html)
- [bash(1) at man7.org, section EXPANSION, Parameter Expansion and the ${parameter:?word} form](https://man7.org/linux/man-pages/man1/bash.1.html#EXPANSION)

## Related articles

- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I disable SSH password login safely?](https://valakes.com/learn/disable-ssh-password-login/): Put PasswordAuthentication no in a file in /etc/ssh/sshd_config.d/ whose name sorts first, run sudo sshd -t, then reload ssh. Keep your current session open and test a new login in a second window before you close it.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
