rmdir
Overview
The rmdir
command removes empty directories. It’s a safer alternative to rm -r
as it only removes directories that contain no files or subdirectories.
Syntax
rmdir [options] directory...
Common Options
Option | Description |
---|---|
-p |
Remove directory and its ancestors |
--ignore-fail-on-non-empty |
Ignore directories containing files |
-v |
Verbose mode |
--parents |
Remove directory and its ancestors |
--help |
Display help message |
--version |
Output version information |
Key Use Cases
- Remove empty directories
- Clean up directory structure
- Remove directory hierarchies
- Safe directory removal
- Directory structure verification
Examples with Explanations
Example 1: Basic Usage
rmdir empty_directory
Remove a single empty directory
Example 2: Remove Parent Directories
rmdir -p parent/child/grandchild
Remove nested empty directories
Example 3: Verbose Removal
rmdir -v directory
Show what’s being done
Understanding Output
- No output by default
- With -v:
- ‘rmdir: removing directory, directory_name’
- Error messages for:
- Directory not empty
- No such file or directory
- Permission denied
- Not a directory
Common Usage Patterns
Remove multiple directories:
rmdir dir1 dir2 dir3
Remove directory tree:
rmdir -p a/b/c
Check if empty:
rmdir directory 2>/dev/null
Performance Analysis
- Fast operation
- Directory entry updates
- Parent directory modification
- Permission checking
- Error handling overhead
Additional Resources
Safety Features
- Only removes empty directories
- Prevents accidental deletion of files
- Can remove directory hierarchies safely
- Provides clear error messages
- No force option available