如何在 Linux 中使用 find、xargs、grep 移动包含字符串的文件

如何在 Linux 中使用 find、xargs、grep 移动包含字符串的文件

我有许多 php 文件,可能超过三百万个。
我如何使用 grep 和 xargs 以及使用 mv 来查找包含字符串的文件,如果它们匹配,则将它们移动到目录中。

我尝试

find . -iname \*.php\* | xargs -n64 grep -hEl 'to times' | xargs -0 -I {} mv {} old/

什么也不做。

但是如果我没有第二个 xargs 运行,我会得到这样的结果......正确的文件

./20150713094500*/vb/showthread.php?p=10297131
./20150715080347*/vb/showthread.php?t=1133875&page=3
./20150630233104*/vb/member.php?find=lastposter&t=1234502

答案1

查找匹配的文件'*.php*

find . -type f -name '*.php*' |

然后将每个文件名传递给该文件名,grep如果找到“to times”字符串,则仅输出文件名

xargs -I {} grep -l 'to times' '{}' |

然后将每个文件名传递给mv

xargs -I {} mv '{}' old/

相关内容