[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