Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Linux

Delete directories in Linux safely: rmdir, rm -r and find

Remove empty and full directories in Linux with rmdir, rm -r and find, add safety rails against rm -rf accidents, and fix permission denied and busy errors.

CloudSat teamNovember 18, 20266 min read
Abstract illustration of a folder tree with one branch dissolving into small particles while a shield shape guards the root
On this pageLook before you delete

On a Linux server there is no recycle bin. rm unlinks files immediately, and on ext4 or XFS you should treat that as final. Deleting a directory is easy; deleting exactly the directory you meant, and nothing else, is the part that deserves a method. This guide covers the right command for each case, the rails that stop a typo from wiping a system, and the errors you will meet along the way.

Look before you delete

Spend ten seconds checking what is inside and how big it is:

ls -la /srv/app/releases/old
du -sh /srv/app/releases/old
find /srv/app/releases/old -type f | wc -l
findmnt -R /srv/app/releases/old

The last command matters more than it looks. If another filesystem is mounted somewhere below the directory, such as a data volume or a network share, a recursive delete walks into it and removes its contents too. findmnt -R prints nothing when no mount is involved.

Pick the command

Goal Command
Remove an empty directory rmdir dir or rm -d dir
Remove a chain of empty directories rmdir -p a/b/c
Remove a directory and everything in it rm -r dir
The same, with one confirmation prompt rm -rI dir
The same, with no prompts at all rm -rf dir
Empty a directory but keep it find dir -mindepth 1 -delete
Delete only what matches a name, age or type find dir ... -delete

Empty directories: rmdir

rmdir only removes empty directories, and that limitation is the point: it cannot destroy data by accident. If it reports Directory not empty on a folder that looks empty, there are hidden files:

ls -A cache/
rmdir -p build/tmp/cache

rmdir -p removes cache, then tmp, then build, stopping at the first one that still holds something. rm -d does the same job as plain rmdir and fits better in scripts that already use rm.

Full directories: rm -r

rm -r removes a directory tree. The flags change how much it asks:

  • -i prompts for every single file, which nobody answers honestly past the tenth one.
  • -I prompts once before a recursive delete, which is the useful middle ground.
  • -f never prompts and ignores files that do not exist.
  • -v prints each removal, handy when you want a record.
rm -rI /srv/app/releases/old

Without -f, rm stops to ask about write-protected files (remove write-protected regular file?). That question is often a sign you are deleting something another user or service owns, so read it before you answer.

Keep the folder, remove what is inside

rm -rf /var/cache/app/* looks right but has two flaws: the glob skips hidden files, and in a directory with hundreds of thousands of entries it fails with Argument list too long. Let find do it, previewing first:

find /var/cache/app -mindepth 1 -print | head
find /var/cache/app -mindepth 1 -delete

-mindepth 1 protects the starting directory itself. -delete processes children before parents, so directories are empty by the time it reaches them.

Targeted deletes with find

find decides what goes by name, age, type or depth. Always run the command with -print first, read the list, then swap in -delete.

find /var/log/app -type f -name '*.log' -mtime +30 -print
find /var/log/app -type f -name '*.log' -mtime +30 -delete
find /srv/uploads -type d -empty -delete
find /srv/data -mindepth 1 -maxdepth 1 ! -name keep -exec rm -rf {} +

The third line clears out empty folders left behind by an app. The fourth removes everything at the top level of /srv/data except the entry called keep. Avoid combining -prune with -delete: -delete implies depth-first processing, which makes -prune ineffective. Add -xdev to keep find on one filesystem, and never pipe ls into xargs rm, since names with spaces break it. If you need xargs, use find ... -print0 | xargs -0 rm -f.

Safety rails

A few habits remove most of the risk:

  • Rely on --preserve-root. GNU rm refuses to work recursively on / by default. Never add --no-preserve-root.
  • Add --one-file-system to recursive deletes on servers with extra volumes. It skips any directory on a different filesystem from the one you named.
  • Guard variables in scripts. If TARGET_DIR is empty, rm -rf "$TARGET_DIR/build" becomes rm -rf /build. The :? expansion aborts the script instead:
set -euo pipefail
rm -rf -- "${TARGET_DIR:?}/build"
  • Use -- or ./ for odd names. A directory called -old looks like an option; rm -r -- -old or rm -r ./-old removes it.
  • Chain cd with &&. In cd /srv/tmp; rm -rf ./*, a failed cd runs the delete in whatever directory you were already in. cd /srv/tmp && rm -rf ./* stops at the failure.
  • Take a snapshot before a big cleanup. On CloudSat, a snapshot from the console gives you a rollback point in case the wrong tree goes (take a snapshot), and a real backup and restore plan covers the rest.

Errors and what they mean

Error Cause Fix
Directory not empty Hidden files remain ls -A, then rm -r if you meant it
Permission denied No write permission on the parent directory Fix ownership, or use sudo if the files are really yours to remove
Operation not permitted Immutable or append-only attribute lsattr -d path, then sudo chattr -i path
Device or resource busy The path is a mount point findmnt path, then unmount it first
Read-only file system The filesystem is mounted read-only Find out why (often disk errors) before remounting
Argument list too long A glob expanded to too many names Use find ... -delete

Permission to delete a file comes from the directory that contains it, not from the file itself. That is why you can sometimes delete a file you cannot read, and why a file you own can be impossible to delete from someone else's directory.

To find what keeps a path busy, fuser -vm /mountpoint lists the processes using a mounted filesystem, and lsof +D /path names every process with a file open under a directory. lsof +D reads the whole tree, so it can be slow on large directories.

Trash instead of delete

On a desktop, sending files to the trash gives you an undo. GNOME systems include gio trash, and the trash-cli package adds trash-put, trash-list, trash-restore and trash-empty for any desktop:

gio trash old-project/
trash-put old-project/
trash-list

On a server, a trash folder only moves the space problem somewhere else. Snapshots and backups are the server-grade undo.

FAQ

Can I recover a directory deleted with rm -r?

Rarely, and not reliably. Stop writing to that filesystem at once, because new data overwrites the freed blocks. Recovery tools work on unmounted disks with mixed results. A backup or a snapshot is the only dependable way back.

Why is disk space not freed after I deleted a large file?

A running process still has it open, so the data stays allocated until the process closes it. sudo lsof +L1 lists deleted files that are still open. Restart the service that holds them, and the space comes back.

Is there a difference between rm -r and rm -R?

No. Both mean recursive. rmdir and rm -d, on the other hand, only ever remove empty directories.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.