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
Usage examples
Remove empty directory
rmdir ./empty-folderThe target is removed only if it exists and contains no entries.
Remove empty nested structure
rmdir -p ./temp/emptyThe empty target and then eligible empty parent directories are removed; inspect the entire parent path first.
Check content before deleting
ls -la ./empty-folderHidden 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.