Navigation and files

Command rmdir

Delete empty directory

Linux · Bash examples · commands run on the selected server

When to use rmdir

Use rmdir when you want to remove only empty directories. Its refusal to delete a nonempty directory is useful protection when cleaning temporary paths without recursively removing their contents.

Syntax

rmdir [PARAMETERS] DIRECTORY…

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-p
Also delete empty parent directories.
-v
Show deleted directories.
--ignore-fail-on-non-empty
Don't treat a non-empty directory as an error in GNU rmdir.

Usage examples

Remove empty directory

rmdir ./empty-folder

The target is removed only if it exists and contains no entries.

Remove empty nested structure

rmdir -p ./temp/empty

The empty target and then eligible empty parent directories are removed; inspect the entire parent path first.

Check content before deleting

ls -la ./empty-folder

Hidden entries are included in the inspection, which can explain why a directory is not empty.

Notes and limitations

  • Only empty directories can be removed; even hidden files make a directory non-empty.
  • The -p option can remove several parent directories. Use a narrowly scoped relative path.

Common questions and troubleshooting

Why does an apparently empty directory remain?

Hidden files or other entries still count as contents. Inspect with a listing that includes dotfiles before deciding what to remove.

Should I replace rmdir with recursive rm?

Only when deleting the contents is actually intended and backed up. A nonempty-directory error is a reason to inspect, not automatically to escalate deletion.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man rmdir.