[root@sqj2015 test]# pwd
/root/test
[root@sqj2015 test]# ls
01.txt 02.txt a b
现在要删除除 01.txt 之外的所有档案, 具体方法若下:
[root@sqj2015 test]# rm -rf !(01.txt)
-bash: !: event not found
提示错误, 解决方法是新增下变数:
[root@sqj2015 test]# shopt -s extglob
然后在执行上面的命令则可以:
[root@sqj2015 test]# rm -rf !(01.txt)
[root@sqj2015 test]# ls
01.txt
如果是多个要排除的, 可以这样:
[root@sqj2015 test]# ls
01.txt 02.txt a b
[root@sqj2015 test]# rm -rf !(01.txt|a)
[root@sqj2015 test]# ls
01.txt a
注: 此命令只对当前终端有效, 换个终端或者退出, 需要重新执行能有效.
参考:http://tech.firss.com/shanchu-wnejian.html