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

转载于:https://www.cnblogs.com/Assaltyfish/p/10129902.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值