在目录中,如何删除缺少任何指定单词的文件,以便只留下包含所有单词的文件?我试图使用grep和rm命令编写一个简单的bash shell脚本,但是我迷路了。我对Linux完全陌生,任何帮助都将不胜感激。
发布于 2009-03-05 13:08:48
这样如何:
grep -L foo *.txt | xargs rm
grep -L bar *.txt | xargs rm如果一个文件不包含foo,那么第一行就会删除它。
如果一个文件不包含bar,那么第二行将删除它。
只应保留同时包含foo和bar的文件
-L, --files-without-match
Suppress normal output; instead print the name of each input
file from which no output would normally have been printed. The
scanning will stop on the first match.有关放置在循环中的信息,请参见@Mykola Golubyev's post。
发布于 2009-03-05 13:22:16
list=`Word1 Word2 Word3 Word4 Word5`
for word in $list
grep -L $word *.txt | xargs rm
done发布于 2009-03-05 13:28:40
除了上面的答案:使用换行符作为分隔符来处理带有空格的文件名!
grep -L $word $file | xargs -d '\n' rmhttps://stackoverflow.com/questions/614654
复制相似问题