The Boring Infra ▍

LINUX · COMMAND LINE

How to remove a directory on linux

17 November 2026 · 6 minute read

Deleting a directory is the command everyone learns twice. The first time is rmdir, which fails the moment the directory has anything in it, and the message reads like the tool is broken. The second time is rm -r, which works and keeps working right up to the afternoon a typo puts it on the wrong path. I've watched a script delete a build directory that turned out to be a mount point. rm -r emptied it and then failed at the last step, because a mount point can't be removed while something is mounted on it. rm -r --one-file-system is the flag that stops it walking into the mount at all.

Run rmdir dirname to remove an empty directory, and rm -r dirname to remove a directory with anything inside it. Add -i to be asked about each file or -I to be asked once, because nothing here goes to a trash can.

rmdir removes an empty directory

rmdir does one job. It removes a directory that holds nothing, and it refuses anything else.

$ rmdir work/empty
$ ls work
full
nested

It removed the directory and printed nothing. The refusal is the part worth having.

$ rmdir work/full
rmdir: failed to remove 'work/full': Directory not empty
$ echo $?
1

That's not an error you need to fix. It's the command doing what it promises. rmdir can't take contents with it, so pointing it at the wrong directory costs you nothing, and it exits 1 so a script notices.

One flag extends it. -p walks up the path after removing the last directory, so rmdir -p s/t/u removes u, then t, then s. Each level still has to be empty, and the command stops at the first one that isn't.

$ mkdir -p s/t/u
$ rmdir -p s/t/u ; echo $?
0

$ mkdir -p q/keep/x ; touch q/keep/file.txt
$ rmdir -p q/keep/x ; echo $?
rmdir: failed to remove directory 'q/keep': Directory not empty
1

The first run removed all three directories and reported zero. The second removed x, then stopped at keep because it still held a file, which left q/keep and the file in place.

rm -r removes a directory and its contents

For a directory with contents you need rm and the recursive flag. rm on its own refuses directories, so -r is what tells it to walk the tree.

$ rm -r work/nested
$ ls work
full

A file and three directories gone in one command, with no prompt and no record. -v gives you the record.

$ rm -v work/keep/z.txt
removed 'work/keep/z.txt'

-v prints one line per removal, which is the difference between a cleanup you can describe to someone later and one nobody can reconstruct. find has an equivalent that works from a filter instead of a path.

$ mkdir -p work/keep work/old work/tmp && touch work/keep/report.txt
$ find work -type d -empty -delete
$ find work
work
work/keep
work/keep/report.txt

Both empty directories went, the one with a file survived, and find reached them by test rather than by name. On a directory tree you don't fully remember, that's safer than a wildcard.

Remove a single file

The plain command deletes files, and it won't touch a directory without -r or -d.

$ rm work/full/f.txt
$ ls work/full

Empty output, because the directory is now empty. -d is the middle ground between rm and rmdir. It removes an empty directory with rm, which saves a second command when you are already typing rm.

Ask before you delete

rm has two prompts and they behave differently. -i asks per file.

$ echo q > work/x.txt
$ rm -i work/x.txt
rm: remove regular file 'work/x.txt'? n
$ ls work
full
x.txt

The answer was no and the file stayed. -I asks once, and only when the command is big enough to matter, meaning more than three files or anything recursive.

$ rm -I junk/*.txt
rm: remove 4 arguments? n
$ ls junk
four.txt
one.txt
three.txt
two.txt

Four files, one question, all four kept. Four is deliberate, because -I only asks at four or more arguments, or on any recursive delete. At three it says nothing and deletes them all, so it's not a guard for small deletes. That's the prompt I put on rm -r lines in routines I run by hand. One question per session catches the wrong path, and it never trains me to type y on reflex.

Which one to use, at a glance

CommandWhat it doesReach for it when
rmdir dirRemoves an empty directoryThe directory has nothing in it and you want it to stay that way
rmdir -p a/b/cRemoves a chain of empty directoriesYou just emptied a nested path
rm fileRemoves a fileOne file, known path
rm -r dirRemoves a directory and everything under it. Add --one-file-system when a mount can sit inside the treeThe contents should go with the directory
rm -iAsks about every fileYou can't get the file back by downloading it again
rm -IAsks once for a big or recursive deleteYou want one guard on a recursive command
rm -rfRecursive, no prompts, no complaintsYou are certain, and mostly you should not be
find . -empty -deleteRemoves matches by testThe tree is unknown and you want a filter

Verify it works

Build a fresh tree for this, since the sections above already deleted theirs.

$ mkdir -p work/empty work/full work/nested/a/b
$ touch work/full/f.txt work/nested/a/b/deep.txt

$ rmdir work/empty ; echo $?
0

$ rmdir work/full ; echo $?
rmdir: failed to remove 'work/full': Directory not empty
1

$ rm -r work/nested ; echo $?
0

$ ls work
full

The first and third succeed quietly and report zero. The middle one is the one to learn, a directory with contents refused with a clear reason and a status of one. The last listing shows the nested tree gone and the directory with the file in it untouched. If rmdir ever succeeds where you expected a refusal, the directory was empty and something didn't get created.

The honest caveat

There's no trash can. rm unlinks the file and frees its blocks, and there's no undo, though lsof +L1 names any process still holding a deleted file open, which is why the space sometimes stays gone. On ext4 there are tools like extundelete that read the raw device, and they're unmaintained, ext4 only, and not shipped with RHEL. On XFS there's nothing to reach for at all. Your recovery plan is a backup, an LVM snapshot, or a snapshot from the VM or the storage array, which is why taking one before a cleanup is the habit that matters.

rm -- -f removes a file literally named -f, since the double dash ends option parsing. On the Kubernetes nodes I look after, a directory under /var/lib that you delete while a process holds a file open doesn't give the space back until that process closes the handle. The df number stays flat and the next question is always which process is still holding it.

For anything scripted, one rule has saved me more than any flag. A variable in the path goes wrong two ways. If it's unset, the shell expands nothing and the command deletes the wrong thing. If it ends up empty with a trailing slash, it points at the root of the filesystem. ${DIR:?} stops the script when the variable is empty, and one test against / stops the second case. The flag that keeps rm -rf quiet is the same flag that removes the confirmation which would have caught the mistake.

The boring conclusion

Empty directories come off with rmdir, everything else with rm -r, and neither has an undo. Read the path, list what's inside, then delete. -I on the recursive ones is a cheap habit.

What's the deletion you still think about?

Frequently asked questions

How do I remove a directory in Linux?

Run rmdir dirname if the directory is empty, and rm -r dirname if it has files inside. rmdir refuses anything with contents, so it won't delete more than you expected.

What is the difference between rmdir and rm -r?

rmdir removes one empty directory and nothing else. rm -r walks the tree and removes the directory plus everything inside it. rmdir is the safe one because it fails instead of deleting contents.

Why does rmdir say directory not empty?

Because the directory still has files or subdirectories in it. Either empty it first or use rm -r, which removes the contents and then the directory.

How do I delete a directory without being asked about every file?

Run rm -r dirname. It removes everything under the path without prompting. Add -I if you want one confirmation before a recursive delete runs.

Can I undo rm?

Not with rm. The file is unlinked and there's no undo. On ext4 there are tools like extundelete that read the raw device, and they're unmaintained, ext4 only, and not shipped with RHEL. On XFS nothing reaches for it at all, so the recovery plan is a backup or a snapshot.

What does the f in rm -rf do?

It ignores files that don't exist and never prompts. It doesn't skip permission checks or make the command safer. rm -rf on a wrong path is the classic way to lose a system.

Resources

These are the references the commands follow, so the behaviour here matches your machine.

This builds on how to rename a file on linux, since the file you misnamed is usually the one you then want gone. Next up is looking at the disks themselves with lsblk.

Next in the series. How to list block devices with lsblk.

The target you are about to remove usually came out of a find run.