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.txt b.txt c.txt test.sh
root@oldboy test$sh test.sh
root@oldboy test$ls
a.txt b.txt test.sh
本文转自 xoyabc 51CTO博客,原文链接:http://blog.51cto.com/xoyabc/1678281,如需转载请自行联系原作者