rmdir command is used to remove empty directories in Linux. This command only removes empty directories, which means that it will not delete any directories that contain files or subdirectories. If you attempt to delete a non-empty directory using
rmdir, you will receive an error message.
The basic syntax for the
rmdir command is as follows:
rmdir [OPTION]... DIRECTORY...
DIRECTORY refers to the name of the directory or directories that you want to remove. You can specify multiple directories to be removed at once by separating their names with spaces.
For example, to remove a directory named
mydir, you would use the following command:
If you want to remove multiple directories at once, you can include their names as separate arguments:
rmdir dir1 dir2 dir3
If the directory that you want to remove is not located in your current working directory, you can specify its full path:
rmdir does not provide any output when it successfully removes a directory. However, you can use the
-v option to display a message for each directory that is removed:
rmdir -v mydir
Specific Use Cases
Here are a few examples of how you might use the
rmdir command in practice:
- If you have a directory that you no longer need, you can use
rmdirto remove it. For example, if you have a directory named
oldstuffthat you want to delete, you would use the following command:
- If you have a script that creates temporary directories during its operation, you can use
rmdirto remove those directories once the script has completed. For example, if your script creates a directory named
tempdir, you could include the following command at the end of the script to remove it:
- If you want to remove multiple directories at once, you can include their names as separate arguments. For example, if you want to remove directories named
dir3, you could use the following command:
rmdir dir1 dir2 dir3.
Here are the available options for the
||Remove DIRECTORY and its ancestors.|
||Output a message for each directory that is removed.|
||Display help information and exit.|
||Display version information and exit.|
If you attempt to remove a non-empty directory using
rmdir, you will receive an error message. To remove a directory that contains files or subdirectories, you should use the
rm command instead.
If you receive a message that says “Directory not empty” when trying to remove a directory using
rmdir, it means that the directory is not empty and cannot be removed. You can use the
ls command to list the contents of the directory and determine what files or subdirectories are preventing it from being removed.
- Be careful when using the
rmdircommand, as it will permanently delete any empty directories that you specify. Make sure that you have a backup of any important files or directories before using this command.
- If you want to remove a directory and all of its contents, including non-empty subdirectories, you should use the
rmcommand with the