Archives and disk tools

Linux commands: rmdir

Learn how the Linux rmdir command removes empty folders, one or many at a time, and why you need rm -rf instead to delete a folder that still has files.

8 minute lesson

~~~

rmdir removes empty directories:

mkdir fruits
rmdir fruits

It can remove several empty directories:

mkdir fruits cars
rmdir fruits cars

If a directory contains a file, another directory, or sometimes an unseen entry, rmdir fails instead of deleting the contents. Inspect it:

ls -la fruits

This refusal is a safety feature. Remove or relocate known contents first. Recursive deletion with rm -r is a separate, destructive operation; avoid adding -f by habit because it suppresses prompts and some errors.

Before recursive deletion, resolve and print the exact target:

target="$PWD/build-output"
printf 'target: %s\n' "$target"
find "$target" -maxdepth 2 -print

Only proceed after confirming that the resolved path is narrow and expected. Avoid unquoted variables, globs you have not previewed, and commands run from an uncertain working directory. Command-line deletion often bypasses the desktop trash, so recovery may require a backup.

Some implementations support rmdir -p a/b/c, which removes c and then empty parent directories. Read the local manual before using options in a portable script.

Try it: create one empty directory and one containing a file. Run rmdir on both and explain why one refusal protects data.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →