在目录中,如何删除缺少任何指定单词的文件,以便只留下包含所有单词的文件?我尝试使用 grep 和 rm 命令编写一个简单的 bash shell 脚本,但我迷路了。我是 Linux 的新手,我们将不胜感激
最佳答案
怎么样:
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用于放置在一个循环中。
关于Linux:删除不包含所有指定单词的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/614654/