How to use the command rmdir (with examples)
- Linux
- December 25, 2023
The rmdir
command is used to remove directories without files. It is often used when you want to delete empty directories from your system. It is a safer option than the rm
command as it does not delete files, only empty directories.
Use case 1: Remove specific directories
Code:
rmdir path/to/directory1 path/to/directory2 ...
Motivation: You want to remove specific directories from your system that are empty.
Explanation: The rmdir
command followed by the path to the directories you want to remove. You can specify multiple directories by separating them with a space.
Example output:
$ rmdir empty_dir
$ rmdir empty_dir/subdirectory
In this example, the directories “empty_dir” and “empty_dir/subdirectory” are removed from the system as they are empty.
Use case 2: Remove specific nested directories recursively
Code:
rmdir --parents path/to/directory1 path/to/directory2 ...
Motivation: You want to remove specific nested directories and all their parent directories that are empty.
Explanation: The rmdir
command with the --parents
option is used to remove a directory and all its parent directories if they are empty. This is useful when you have nested directories and you want to remove them along with their parent directories if they are empty.
Example output:
$ rmdir --parents empty_dir/nested_dir
$ rmdir --parents empty_dir
In this example, the directories “empty_dir/nested_dir” and “empty_dir” are removed from the system along with their parent directories if they are empty.
Conclusion:
The rmdir
command is a useful tool for removing empty directories from your system. It provides options to remove specific directories or nested directories along with their parent directories recursively if they are empty. Use this command when you want to clean up your system by removing empty directories.