Linux rmdir command: Delete empty directories.
Linux rmdir command Function Description
Use the rmdir command to delete empty directories on a Linux system.
The rmdir command is used to delete empty directories. When a directory is no longer in use, or when disk space has reached the usage limit, it is necessary to delete the directory that has lost its use value. Use the rmdir command to remove one or more empty subdirectories from a directory. This command removes one or more subdirectories from a directory, where dirname is the directory name. If no path is specified in dirname, the directory specified by dirname in the current directory is deleted; if dirname contains a path, the directory in the specified location is deleted. When deleting a directory, you must have write permission to its parent directory.
The subdirectory should be empty before it is deleted. That is, all files in that directory must be all with the rm command. In addition, the current working directory must be above the deleted directory, not the deleted directory itself, and not a subdirectory of the deleted directory.
While it is also possible to recursively delete all files in a directory and the directory itself with the rm command with the -r
option, there is a significant danger in doing so.
Linux rmdir command Syntax
rmdir [option] [directory]
The meaning of each option in the command is shown in the table.
Option | Description |
---|---|
-p or --parents |
Recursive directory deletion, when a subdirectory is deleted and its parent directory is empty, it will also be deleted. |
-v or -verboes |
Showing the detailed execution of the command. |
--ignore-fail-on-non-empty |
This option causes the rmdir command to ignore error messages caused by the deletion of non-empty directories. |
Linux rmdir command Examples
Delete empty directory aaa-apidemos
rmdir aaa-apidemos
Output:
Also delete the empty directories /root/newdir2 and /root/newdir2/newdir3
[root@rhel ~]# rmdir -p /root/newdir2/newdir3
rmdir: Delete directory "/root" Failed: Directory is not empty
Above command will delete the empty directories /root/newdir2 and /root/newdir2/newdir3, but since the /root directory is not empty, the /root directory cannot be deleted, while the other two directories have been deleted.