Today we will learn about the command in linux: rmdir
command.
rmdir
is a commonly used command, the function of this command is to delete empty directories, a directory must be empty before it can be deleted. (Note that the rm - r dir
command can be used instead of rmdir
, but with great danger.) When deleting a directory, you must also have write access to the parent directory.
1. Command format.
rmdir [option]... directory...
2. Command function.
The command removes one or more subdirectory entries from a directory. When deleting a directory, you must also have write access to the parent directory.
3. Command parameters.
-p recursively deletes the directory
dirname
, and when a subdirectory is deleted and its parent directory is empty, it is also deleted together. If the whole path is deleted or part of the path is kept for some reason, the system displays the corresponding message on the standard output.-v, –verbose show the command execution process
4. Examples of commands.
Example 1: rmdir cannot delete non-empty directories
Command: rmdir doc
Output.
[root@localhost scf]# tree
|-- bin
|-- lib
|-- doc
|-- info
|-- product
|-- logs
|-- info
|-- product
|-- service
|-- deploy
|-- info
|-- product
12 directories, 0 files
[root@localhost scf]# rmdir doc
rmdir: doc: directory is not empty
[root@localhost scf]# rmdir doc/info
[root@localhost scf]# rmdir doc/product
[root@localhost scf]# tree
|-- bin
|-- doc
|-- lib
|-- logs
|-- info
|-- product
|-- service
|-- deploy
|-- info
|-- product
10 directories, 0 files
Note: The rmdir
directory name command cannot directly delete non-empty directories.
Example 2: rmdir -p If a subdirectory is deleted to make it empty, it will be deleted as well
Command.
rmdir -p logs
Output.
[root@localhost scf]# tree
|-- bin
|-- doc
|-- lib
|-- logs
|-- product
|-- service
|-- deploy
|-- info
|-- product
10 directories, 0 files
[root@localhost scf]# rmdir -p logs
rmdir: logs: directory is not empty
[root@localhost scf]# tree
|-- bin
|-- doc
|-- lib
|-- logs
|-- product
|-- service
|-- deploy
|-- info
|-- product
9 directories, 0 files
[root@localhost scf]# rmdir -p logs/product
[root@localhost scf]# tree
|-- bin
|-- doc
|-- lib
|-- service
|-- deploy
|-- info
|-- product
7 directories, 0 files
Reference: