1、创建测试文件:
[root@centos7 test]# touch {a..f}.txt
[root@centos7 test]# ls
a.txt b.txt c.txt d.txt e.txt f.txt
method1:
[root@centos7 test]# rm !("b.txt")
[root@centos7 test]# ls
b.txt
method2:
[root@centos7 test]# touch {a..f}.txt [root@centos7 test]# ls a.txt b.txt c.txt d.txt e.txt f.txt [root@centos7 test]# find ./ -not -name "b.txt" -exec rm -f {} \; rm: cannot remove ‘./’: Is a directory [root@centos7 test]# ls b.txt
method3:
[root@centos7 test]# touch {a..f}.txt [root@centos7 test]# ls a.txt b.txt c.txt d.txt e.txt f.txt [root@centos7 test]# ls | grep -v "b.txt" | xargs rm -f [root@centos7 test]# ls b.txt
[root@centos7 test]# touch {a..f}.txt [root@centos7 test]# ls a.txt b.txt c.txt d.txt e.txt f.txt [root@centos7 test]# rm !("b.txt"|"c.txt") [root@centos7 test]# ls b.txt c.txt
[root@centos7 test]# touch {a..f}.txt [root@centos7 test]# ls a.txt b.txt c.txt d.txt e.txt f.txt [root@centos7 test]# find ./ -type f -not -name "b.txt" -not -name "c.txt" -exec rm -f {} \; [root@centos7 test]# ls b.txt c.txt
[root@centos7 test]# touch {a..f}.txt [root@centos7 test]# ls a.txt b.txt c.txt d.txt e.txt f.txt [root@centos7 test]# ls | grep -E -v "b.txt|c.txt" | xargs rm -f [root@centos7 test]# ls b.txt c.txt
[root@centos7 test]# touch {a..d}.txt {a..d}.csv {a..d}.png
[root@centos7 test]# ls
a.csv a.png a.txt b.csv b.png b.txt c.csv c.png c.txt d.csv d.png d.txt
[root@centos7 test]# rm !(*.txt)
[root@centos7 test]# ls
a.txt b.txt c.txt d.txt
[root@centos7 test]# touch {a..d}.txt {a..d}.csv {a..d}.png [root@centos7 test]# ls a.csv a.png a.txt b.csv b.png b.txt c.csv c.png c.txt d.csv d.png d.txt [root@centos7 test]# find ./ -type f ! -name "*.txt" -exec rm -f {} \; [root@centos7 test]# ls a.txt b.txt c.txt d.txt
[root@centos7 test]# touch {a..d}.txt {a..d}.csv {a..d}.png [root@centos7 test]# ls a.csv a.png a.txt b.csv b.png b.txt c.csv c.png c.txt d.csv d.png d.txt [root@centos7 test]# rm !(*.txt|*.csv) [root@centos7 test]# ls a.csv a.txt b.csv b.txt c.csv c.txt d.csv d.txt
[root@centos7 test]# touch {a..d}.txt {a..d}.csv {a..d}.png [root@centos7 test]# ls a.csv a.png a.txt b.csv b.png b.txt c.csv c.png c.txt d.csv d.png d.txt [root@centos7 test]# find ./ -type f ! -name "*.txt" ! -name "*.csv" -exec rm -f {} \; [root@centos7 test]# ls a.csv a.txt b.csv b.txt c.csv c.txt d.csv d.txt
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。