在管理中经常想删除除了某个档案之外的所有档案?应该如何实现呢?比如我的目录如下:
[root@localhost aa]# pwd /root/Desktop/aa [root@localhost aa]# ls 1.txt 2.txt 3.txt
我要删除除了 2.txt 之外的所有档案,具体方法如下:
[root@localhost aa]# rm -r !(2.txt) bash: !: event not found
提示错误,在 archlinux 上如上命令 可以使用。我现在的环境是 centos5.5
解决方法是新增 下变数
[root@localhost aa]# shopt -s extglob
然后再使用上面的命令则可以
[root@localhost aa]# rm -rf !(2.txt) [root@localhost aa]# ls 2.txt
如果是多个要排除的,可以这样
[root@localhost aa]# ls 1.txt 2.txt 3.txt [root@localhost aa]# rm -rf !(1.txt|2.txt) [root@localhost aa]# ls 1.txt 2.txt
此命令只对当前终端有效,换个终端或者退出之后,需要重新执行才能有效。。。