rmdir Command: Tutorial & Examples
Delete an empty directory
The rmdir
command is a Linux utility that is used to delete empty directories from the file system. It stands for "remove directory."
To use the rmdir
command, you will need to specify the name of the empty directory that you want to delete. For example, to delete an empty directory named mydir
, you would use the following command:
rmdir mydir
If the directory is not empty, the rmdir
command will display an error message and will not delete the directory. To delete a non-empty directory, you can also use the rm
command with the -r
option to specify that it should operate recursively.
It's important to note that the rmdir
command can be a useful tool for deleting empty directories, but it is not as powerful as the rm
command. Be sure to use it with caution and make sure that you are deleting the correct directories.
There are also options available to the rmdir
command that allow you to force the deletion of directories, even if they are not empty, or to interactively confirm each directory before deleting it.
Here are some of the most commonly used options:
-p
: This option allows you to delete a series of empty directories in a single command. For example, if you have a series of empty directories named dir1
, dir2
, and dir3
, and they are all nested inside each other, you can use the following command to delete them all in one go:
rmdir -p dir1/dir2/dir3
This will delete dir3
and then dir2
, and then dir1
, as long as they are all empty.
-v
: This option causes rmdir
to display a message for each directory that it successfully deletes. This can be helpful if you are deleting a large number of directories and want to see which ones were removed.
--ignore-fail-on-non-empty
: This option allows you to force rmdir
to delete a directory, even if it is not empty. By default, rmdir
will only delete empty directories, but this option allows you to override that behavior. Be aware that using this option can be dangerous, as it can result in the deletion of important files.
--interactive
: This option causes rmdir
to interactively prompt you for confirmation before deleting each directory. This can be helpful if you want to double-check before deleting each directory.