有的时候要删除一些文件比如象1.txt2.txt3.txt一直到1000.txt这样有规律的文件删除前50个保留后面的

最笨的办法就是自己写个脚本截取文件名字的数字,进行比较然后赋值删除。

今天研究了下其实有更简单的方法就是用find的其实shell也不长就一句话而已

先建立1000个txt文件

for i in`seq 1 1000`;do touch $i.txt; done
只保留50的txt后面的删除掉
find -newer 50.txt|xargs -i rm  -rf {}
find \! -newer50.txt -exec rm -rf {} \;
ll | awk '{print $9}' |grep -v 'a*-[1-50].txt' | xargs rm -f

三种写法都可以。第二种貌似高级了点其实就是find里的取反

-newerSupported.Ifthefilespecifiedisasymboliclink,itisalwaysdereferenced.Thisisachange
frompreviousbehaviour,whichusedtotaketherelevanttimefromthesymboliclink;seetheHISTORY
sectionbelow.