The rmdir command in linux USES elaboration

  • 2020-05-13 04:02:19
  • OfStack

In this article, learn the first command in linux: the rmdir command. rmdir is a common command that removes empty directories. A directory must be empty before it is deleted. (note that the rm-r dir command can replace rmdir, but it is very dangerous.) Deleting a directory must also have write permission to the parent directory.

1. Command format:
rmdir [option]... Directory...

2. Command functions:
This command removes one or more subdirectory entries from a directory, and must have write permissions to the parent directory when deleting a directory.

3. Command parameters:
-p recursively deletes the directory dirname. When the subdirectory is deleted and its parent directory is empty, 1 is also deleted. If the entire path is deleted or a portion of the path is retained for some reason, the system displays the corresponding information on standard output.
-v, --verbose shows instruction execution

4. Command example:
Example 1: rmdir cannot delete a non-empty directory
Command:
rmdir doc
Output:


[root@localhost scf]# tree
.
|-- bin
|-- doc
|   |-- info
|   `-- product
|-- lib
|-- logs
|   |-- info
|   `-- product
`-- service
    `-- deploy
        |-- info
        `-- product
 
12 directories, 0 files
[root@localhost scf]# rmdir doc
rmdir: doc: The 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

Description:
The rmdir directory name command cannot delete non-empty directories directly

Example 2: rmdir-p if the subdirectory is also empty after it has been deleted, drop 1 and delete it
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: The 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

Conclusion:
This command is used to delete empty directories. If the directory is not empty, an error will occur. You can use rm to delete files in the directory and then rmdir to delete the directory. You can also use the rm-rf command instead of the rmdir command. This is a very simple command.


Related articles: