rgewr2 发表于 2015-7-27 09:24:12

删除目录中不包含xxx字符创的文件

                      a.txt,b.txt,c.txt文件内容如下
root@oldboy test$cat a.txt
sada
xxx
qjieinnxxx
root@oldboy test$cat b.txt
dsfsgxxx
xxx
sadsfsge
root@oldboy test$cat c.txt
adsf
asfgegr
afagsd


法一


1
for file in $(ls);do ! grep -lq xxx $file && rm $file;done




法二

1
for file in $(ls);do ! grep -wq xxx $file && echo $file;done




法三

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
for file in $(ls *.txt)
do
   grep -wq xxx $file
   if [ $? -eq 1 ]; then
      rm -f $file
   else
      continue
   fi
done




执行脚本即把c.txt删除。

root@oldboy test$ls
a.txtb.txtc.txttest.sh
root@oldboy test$sh test.sh
root@oldboy test$ls
a.txtb.txttest.sh


                   

页: [1]
查看完整版本: 删除目录中不包含xxx字符创的文件