删除文件时排除特定文件 (转载)

  转自:http://blog.csdn.net/catoop/article/details/8548292

删除文件时排除特定文件
删除当前目录下所有 *.txt文件,除了test.txt
rm `ls *.txt|egrep -v test.txt`
或者
rm `ls *.txt|awk '{if($0 != "test.txt") print $0}'`
排除多个文件
rm `ls *.txt|egrep -v '(test.txt|fff.txt|ppp.txt)'`
---------------
rm -f `ls *.log.1|egrep -v '(access-2010-09-06.log|error-2010-09-06.log)'`
rm -f `ls *.log|egrep -v '(access-2010-09-06.log|error-2010-09-06.log)'`
---------------
rm -f `ls *.log|egrep -v '(20100906.log)'`
---------------
这里是用ls得到原始数据,也可以用find命令
rm `find . -name *.txt | egrep -v ‘(test.txt|fff.txt|ppp.txt)'`

原文地址:https://www.cnblogs.com/lance-ehf/p/5717361.html