Remove an Empty Directory

Remove an Empty Directory

Suppose the teacher created an extra class folder but never put anything inside it. rmdir is a helpful choice when you want to remove that empty folder and leave a nonempty folder alone. Its refusal to remove a populated directory is a useful condition, not an inconvenience to bypass automatically.

Let's practice that distinction. An apparently empty folder can still contain hidden entries, so the important question is whether it has entries at all, not whether a normal file listing happens to display any.

What you’ll learn

  • Remove an empty directory with rmdir.
  • Recognize why a nonempty directory is rejected.

An empty folder can be removed without recursion

  1. Empty directory

    There are no entries to remove inside this folder.

    empty-class/
  2. Remove

    rmdir succeeds only when the directory is empty.

    rmdir empty-class
  3. Boundary

    A folder containing a report is rejected instead of recursively erased.

    nonempty folder → error
Choose rmdir when emptiness is part of your requirement.

Remove an empty class folder

Prepare the example

Work in a new practice folder. The first line creates an empty child directory for this exercise. We are not removing the current directory or a folder containing reports. If you reuse an existing folder, inspect hidden entries as well before deciding it is empty.

Bash · Linux terminal

mkdir empty-class
rmdir empty-class
test ! -d empty-class && printf 'Empty directory removed\n'

Expected result

Empty directory removed

mkdir empty-class creates the test directory, and rmdir empty-class removes it because it contains no entries apart from its special dot entries. The final existence check confirms that the directory path is gone.

Try adding a report to another disposable folder before using rmdir on it. The command should reject that nonempty folder rather than traverse and erase its contents. That behavior is exactly why you might prefer it to recursive removal: the command itself enforces the empty-folder condition you intended.

Empty-folder removal versus recursive removal

Deleting a directory depends on whether it is empty. Linux provides rmdir for empty directories and rm -r when a directory contains files or subdirectories.

Deleting an Empty Directory: rmdir

Let’s start with the simplest case—deleting an empty folder. Linux gives us a command called rmdir just for that.

rmdir myfolder

This will work only if myfolder is completely empty.

rmdir: failed to remove 'myfolder': Directory not empty

See that error? If the folder has even a single file or subfolder inside, rmdir won't work.

Deleting a Directory with Files: rm -r

Now here’s the real deal. If your directory contains files or other folders, use rm with the -r flag. It stands for “recursive”—which means it will go inside and delete everything.

rm -r myfolder

But be careful: this command is powerful. It deletes the folder and everything in it—forever.

Prompt Before Deletion: rm -ri

If you're nervous (which is totally normal), add the -i flag to ask for confirmation before deleting each file:

rm -ri myfolder
rm: descend into directory 'myfolder'? y
rm: remove regular file 'myfolder/file1.txt'? y
rm: remove directory 'myfolder'? y

Great! Now you're in control.

Force Deletion (Use with Caution!): rm -rf

If you're absolutely sure and want to skip all prompts—even if files are read-only—you can use -f for force:

rm -rf myfolder

Warning: This will delete everything inside myfolder without asking. Only use it if you know what you're doing.

Choose the right command for empty and non-empty directories

  • rmdir folder — delete an empty directory
  • rm -r folder — delete a folder and all its contents
  • rm -ri folder — delete interactively with confirmation
  • rm -rf folder — force delete without confirmation

And that’s it! You now know how to safely delete directories in Linux. Practice on test folders first to get comfortable.

Try it, then check the worked answer

Your task: Remove a folder only if it has no entries.

Work in a new practice folder. The first line creates an empty child directory for this exercise. We are not removing the current directory or a folder containing reports. If you reuse an existing folder, inspect hidden entries as well before deciding it is empty.

Bash · complete example for this question

mkdir empty-class
rmdir empty-class
test ! -d empty-class && printf 'Empty directory removed\n'

Expected result

Empty directory removed

Worked answer

rmdir empty-class

Use rmdir empty-class. A recursive rm can remove a nonempty tree, so it changes the condition of the task. If rmdir reports that the directory is not empty, inspect its entries rather than immediately switching to a stronger deletion command.

Check your decision

Remove a folder only if it has no entries.
Choose one answer. You can try again.

Answer and explanation

rmdir empty-class

Use rmdir empty-class. A recursive rm can remove a nonempty tree, so it changes the condition of the task. If rmdir reports that the directory is not empty, inspect its entries rather than immediately switching to a stronger deletion command.

Let rmdir enforce the empty-directory requirement

Choose rmdir when emptiness is part of your requirement. Check hidden entries if the result surprises you. Removing a populated tree is a separate operation with broader consequences, so we will approach it explicitly in its own lesson.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Copy a Directory Tree →