LearnLinux FoundationsManipulation

rmdir

rmdir - a hands-on Linux lab on a real virtual machine.

Meet rmdir, the safe, narrow delete that removes an empty directory only. It succeeds in silence, so you prove each removal with ls. Its defining behavior is refusing a non-empty folder with "Directory not empty". Learn -p to clear an empty chain, -v to narrate each removal, and the three refusals you can read on sight.

You are about to learn a command that deletes things, and yet it is one of the safest commands in Linux. It has a rule so strict that it protects you from your own worst mistakes. It will refuse, flatly, to remove a folder that still has anything inside it.

That refusal is not a flaw. It is the whole point. The command is called rmdir, short for remove directory, and its narrow, stubborn job is to delete a folder only when that folder is already empty. This lesson shows you how it works, why its one big error message is a feature and not a failure, and when you reach for it instead of the heavier delete tools.

The black boxes below are a practice terminal: a safe sandbox that checks the one command each step teaches, so you cannot break anything. rmdir succeeds in silence, which means it prints nothing at all when it works. So after every removal you will run ls to LIST what is left, and the proof that rmdir worked is that the folder's name is simply gone from the list.

A directory is just Linux's word for a folder, a named container that holds files and other folders. rmdir removes one of those containers from the disk. That is the entire command: it takes a directory name and deletes that directory.

But it has one condition, and it never bends: the directory must be empty. Empty means it holds no files and no other folders. If anything is still inside, rmdir stops and tells you so, without deleting a thing. Think of it like a rule that says you cannot throw out a box until you have taken everything out of it first. That rule is what makes rmdir the delete you can run without holding your breath.

rmdir has been a standard Unix command since the 1970s, born alongside mkdir, the command that MAKES a directory. They are a matched pair: mkdir creates the empty folder, rmdir removes the empty folder. The symmetry is deliberate. One builds the box, the other tears it down, and the box must be empty at both ends.

Start with the plain case. Imagine you made a folder called project, used it, emptied it out, and now you want it gone. Because rmdir prints nothing when it succeeds, you cannot trust your eyes on the removal alone. So you will do two things on one line: remove the folder, then immediately ls to list the directory and see that project is no longer there. The ; between them simply means do this, then do that.

Before you run it, decide what you expect: after the removal, the name project should be missing from the listing. Confirm it:

rmdir project; ls

prompt: student@linuxcamp:~$ answer: rmdir project; ls output: notes reports hint: Type rmdir, a space, the folder name, then a semicolon and ls: rmdir project; ls

rmdir itself printed nothing, exactly as promised. The only output you see, notes reports, came from ls, and the important thing about that line is what is NOT in it: project is gone. That silence followed by a shorter listing is what success looks like for rmdir. It does its work quietly and leaves the proof in the listing. From here on, that is the pattern: remove, then ls, then read the absence.

Now the behavior that makes rmdir special. Suppose a folder called full still has a file inside it. Try to remove it and rmdir will not touch it. Instead it prints one clear error and leaves the folder exactly where it was.

Run the removal, then ls to prove the folder survived:

rmdir full; ls

prompt: student@linuxcamp:~$ answer: rmdir full; ls output: rmdir: failed to remove 'full': Directory not empty full notes hint: Same shape as before. rmdir the folder, then ls to see it is still there: rmdir full; ls

Read that error carefully, because you WILL see it often: rmdir: failed to remove 'full': Directory not empty. It means precisely what it says. The folder full still holds at least one file or folder, so rmdir refused and deleted nothing. The proof is the ls line right after: full is still listed. This is not rmdir breaking. It is rmdir protecting you. When you meet this error, the fix is to empty the folder first, then remove it, or reach for a heavier tool you will meet soon.

Sometimes you build a nested stack of folders, like a holding b holding c, and later you want the whole empty stack gone. Running rmdir three times would work, but there is a flag for it. The -p flag stands for parents, and it tells rmdir to also remove the empty parent directories above the one you name, walking up the chain.

So rmdir -p a/b/c removes c, then b, then a, as long as each one is empty once its child is gone. Run it, then ls to prove the entire chain vanished, not just the deepest folder:

rmdir -p a/b/c; ls

prompt: student@linuxcamp:~$ answer: rmdir -p a/b/c; ls output: notes reports hint: Add the -p flag, then the full nested path with slashes: rmdir -p a/b/c; ls

The whole stack is gone. Without -p, rmdir a/b/c would have removed only c, leaving the empty a and b behind for you to clean up by hand. With -p, rmdir climbed the chain: it removed c, saw that b was now empty and removed it, saw that a was now empty and removed it too. The ls confirms it: no a in the listing at all. One honest limit still holds at every rung. If any folder in the chain were not empty, rmdir would stop climbing at that point and print the same Directory not empty refusal.

Because rmdir works in silence, there are moments you want it to narrate what it did, especially when removing a chain. The -v flag stands for verbose, and it makes rmdir print one line for each directory it removes. The removal is identical; you just get a receipt.

Remove an empty folder called gone with -v, then ls to confirm both the receipt and the result:

rmdir -v gone; ls

prompt: student@linuxcamp:~$ answer: rmdir -v gone; ls output: rmdir: removing directory, 'gone' notes reports hint: Add the -v flag before the folder name: rmdir -v gone; ls

This time rmdir spoke. The line rmdir: removing directory, 'gone' is rmdir telling you, step by step, what it removed. Do not let the word rmdir: at the front fool you, this is not an error. Errors say failed to remove; this says removing directory, which is the sound of success narrated out loud. The ls line proves it: gone is no longer listed. Pair -v with -p on a deep chain and you get one receipt line per folder as rmdir climbs, which is a satisfying way to watch the whole stack disappear.

Besides Directory not empty, rmdir has two other short errors you will meet. Both are it refusing to guess, and both are byte for byte what the machine prints:

The message rmdir: failed to remove 'ghost': No such file or directory means there is no folder by that name to remove. Check your spelling first, then run ls to see the real names present. Most often this is a typo or you are standing in the wrong directory.

The message rmdir: failed to remove 'afile': Not a directory means the name you gave exists, but it is a plain file, not a folder, and rmdir only removes folders. Check with ls -l: a folder's line begins with d, a file's line begins with -. If you meant to delete a file, rmdir is the wrong tool for that.

rmdir deletes empty folders only, and never files. To remove a folder that still has things inside it, you will use rm -r (remove, recursively), a heavier command with a full lesson of its own coming up. rmdir is the careful scalpel; rm -r is the power tool. Reaching for rmdir first is a good habit, because its refusal to delete a non-empty folder has saved countless people from wiping out work they meant to keep.

Scaffolding off. No command is printed this time. You have every piece you need.

You made a temporary folder named scratch, you have emptied it out, and now you want it gone. You do not want any other tool, just the careful one that removes an empty directory. Remove scratch, and on the same line list what is left so you can see, with your own eyes, that scratch is no longer there.

prompt: student@linuxcamp:~$ answer: rmdir scratch; ls output: notes reports hint: Use the remove-directory command on scratch, then the semicolon-and-ls proof you have used all lesson: the command, the folder, a semicolon, then ls.

That is the everyday move. rmdir scratch removed the empty folder in silence, and ls gave you the proof by NOT listing scratch anymore. You recalled the command, the folder name, and the reveal trick from memory, which is exactly the recall the real machine will ask of you. If scratch had still held a file, you would have met Directory not empty instead, and you would have known at once that the folder was not actually empty yet.

You earned this cheat sheet. Every row is a form of rmdir you just ran:

And the three refusals you can now read on sight: Directory not empty (still has contents), No such file or directory (no such name, check spelling), and Not a directory (that name is a file, not a folder). Because rmdir is silent when it works, the habit that never fails you is to follow it with ls and read what is missing.

When you want a folder gone but it still has contents, empty it first and let rmdir finish the job, or use rm -r from the lesson ahead. Keep rmdir as your default when you believe a folder is already empty. If you are wrong, it tells you plainly instead of deleting anything, and that is the safest kind of delete there is.

The practice terminal has shown you the whole of rmdir. It removes an empty directory in silence, -p clears an empty chain, -v narrates each removal, and it flatly refuses a non-empty folder with Directory not empty. Every one of those you typed yourself, and you proved each removal with ls.

The Manipulation module ends with one real Linux machine, the module capstone mission, and that is where you put these pieces to work for real. A VM boots just for you, with a live shell and real folders to clear. It hands you objectives that use exactly what you practiced across this module: making, copying, moving, and removing files and folders. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.

Finish the other Manipulation lessons, then go clear a real empty directory for yourself.

Practice rmdir in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.