rmdir命令用来删除空目录。做运维工作的朋友都知道rm命令是危险的,但是关于rm命令误删除的新闻却屡见不鲜。多种情况下因为在使用rm命令时使用了-f选项,如果在执行删除操作时,只使用rm命令的-r选项与rmdir命令组合使用,虽然相比操作要复杂些,但是可以有效的避免误删除。
注意:rmdir命令在删除目录时,其子目录必须是空的,需使用rm命令先删除目录中的文件。此外,当前目录必须在要删除的目录之上,否则会报错。
语法
rmdir [选项] [参数]
选项
-r 此选项可以递归删除一个目录中的所有文件和该目录本身(不建议使用);
-p 在删除指定目录后,若上层目录也为空目录,也一并删除;
--version 显示命令的版本信息。
参数
目录:指定要显删除的目录。
实例
[root@itkaifang ~]# tree a a `-- b `-- c 2 directories, 0 files [root@itkaifang ~]# rmdir a/b/c [root@itkaifang ~]# ls a anaconda-ks.cfg Desktop [root@itkaifang ~]# tree a a `-- b 1 directory, 0 files
-p选项的应用:
[root@itkaifang ~]# tree a a `-- b 1 directory, 0 files [root@itkaifang ~]# rmdir -p a/b [root@itkaifang ~]# ls anaconda-ks.cfg Desktop [root@itkaifang ~]#
[root@itkaifang ~]# rmdir --version rmdir (GNU coreutils) 5.97 Copyright (C) 2006 Free Software Foundation, Inc. This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent permitted by law. Written by David MacKenzie.