Linux:删除不包含所有指定单词的文件

标签 linux bash file shell

在目录中,如何删除缺少任何指定单词的文件,以便只留下包含所有单词的文件?我尝试使用 grep 和 rm 命令编写一个简单的 bash shell 脚本,但我迷路了。我是 Linux 的新手,我们将不胜感激

最佳答案

怎么样:

grep -L foo *.txt | xargs rm
grep -L bar *.txt | xargs rm

如果文件包含foo,那么第一行将删除它。

如果文件包含bar,则第二行将删除它。

只有同时包含 foobar 的文件应该被保留

-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/

相关文章:

在多平台上比较 2 struct tm

c - 同时读取两个目录下的所有文件

regex - bash 正则表达式中的匹配组

jquery - 如何克隆值输入文件

jquery - 如何通过查询类来检查 DOM 中的元素是否被隐藏?

linux - 如何使用 grep 查找匹配之前 * 和 * 之后的上下文行?

c++ - Cygwin C++ 开发

java - 如何在 JSch 中检测错误(如 "Directory not found"、 "TNS listener lost contact"等)?

bash - CENTOS apache ALL=(root) NOPASSWD :/path/to/shell. sh 不工作

regex - sed 替换双引号内的内容