Linux-删除某个文件以外的所有文件

[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
注: 此命令只对当前终端有效,换个终端或者退出,需要重新执行能有效.

参考:https://tech.firss.com/shanchu-wnejian.html