linux命令_文件管理_rmdir_删除空目录

命令:rmdir(remove directory)

删除空目录

格式

rmdir [OPTION]...DIRECTORY...

参数说明

  • --ignore-fail-on-non-empty 忽略每个因为非空目录删除失败的目录
  • -p,--parents 删除子目录之后如果父目录也为空,则同时删除父目录
  • -v,--verbose 显示命令的执行过程

使用实例

  1. 删除空目录

     [fish@localhost ~]$ mkdir test
     [fish@localhost ~]$ tree
     .
     └── test
    
     1 directory, 0 files
     [fish@localhost ~]$ rmdir test/
     [fish@localhost ~]$ echo $?
     0
    
  2. 删除父目录中的空目录

     [fish@localhost ~]$ mkdir -p a/b/c
     [fish@localhost ~]$ tree
     .
     └── a
         └── b
             └── c
    
     3 directories, 0 files
     [fish@localhost ~]$ rmdir -pv a/b/c/ # 显示删除过程
     rmdir: removing directory, ‘a/b/c/’
     rmdir: removing directory, ‘a/b’
     rmdir: removing directory, ‘a’
     [fish@localhost ~]$ ll
     total 0
    
  3. 返回值测试

     [fish@localhost ~]$ mkdir -p a/{b/c,d} # 新建测试目录  
     [fish@localhost ~]$ tree
     .
     └── a
         ├── b
         │   └── c
         └── d
    
     4 directories, 0 files
     [fish@localhost ~]$ rmdir -pv a/b/c/  # 显示删除次序  
     rmdir: removing directory, ‘a/b/c/’
     rmdir: removing directory, ‘a/b’
     rmdir: removing directory, ‘a’
     rmdir: failed to remove directory ‘a’: Directory not empty
     [fish@localhost ~]$ echo $?  # 执行mkdir a 时,因为存在a/d,故删除失败  
     1
     [fish@localhost ~]$ tree
     .
     └── a
         └── d
    
     2 directories, 0 files
     [fish@localhost ~]$ rmdir -p a/d/ # 删除上次测试的所有文件
     [fish@localhost ~]$ mkdir -p a/{b/c,d} # 重新建立测试
     [fish@localhost ~]$ rmdir -pv --ignore-fail-on-non-empty a/b/c/ # 忽略失败,返回值为0
     rmdir: removing directory, ‘a/b/c/’
     rmdir: removing directory, ‘a/b’
     rmdir: removing directory, ‘a’
     [fish@localhost ~]$ echo $?
     0
     [fish@localhost ~]$ tree
     .
     └── a
         └── d
    
     2 directories, 0 files
    
posted @ 2018-12-17 10:20  简单_知行  阅读(704)  评论(0编辑  收藏  举报