rmdir – Remove Empty Directories
The rmdir command deletes empty directories. It will fail if the directory contains any files or subdirectories.
Syntax
rmdir [OPTION]... DIRECTORY...
- You can specify one or more empty directories to remove.
Common Options
| Option | Description |
|---|---|
-p |
Remove DIRECTORY and its ancestors if they become empty (like a recursive remove) |
--ignore-fail-on-non-empty |
Do not throw an error if the directory is not empty |
Practical Examples
1. Remove a single empty directory
rmdir myemptyfolder
2. Remove multiple empty directories at once
rmdir folder1 folder2 folder3
3. Remove a directory and its parent directories if they are empty
rmdir -p projects/2025/october
This removes october, then 2025 and projects only if they are empty after removing the subdirectories.
4. Ignore errors for non-empty directories
rmdir --ignore-fail-on-non-empty folder
This will not show an error if folder is not empty.
Important Notes
rmdironly removes empty directories. To delete directories with content, userm -rorrm -rf(with caution).- If you want to delete a directory regardless of content,
rmdirwill not work.
Tips
- Use
rmdir -pto clean up nested empty directories easily. - Use
ls -lato check if a directory is really empty before removing it. - For safer deletion of non-empty directories, consider using
rm -rito prompt for each deletion.
See Also
rm -r– remove directories and their contents recursivelymkdir– create directoriesls -ld– list directory information
Summary
rmdir is a simple and safe way to remove empty directories, ideal for cleaning up directory structures without risking data loss.