如何在 find 的 -exec 参数中组合两个命令?

How to combine two commands in -exec parameter of find?

我有这个查找命令来获取在最后 50 秒内修改的所有文件,这些文件在最后 1000 个字符中与以下正则表达式 hell\d 匹配。 我使用 tail 获取最后 1000 个字符以加快搜索速度,因为要检查的文件很大(平均 3gb)。

find /home/ouhma -newermt '50 seconds' -type f |
while read fic; do
    if tail -c 1000 "${fic}" | LANG=C LC_ALL=C grep -Pq 'hell\d'; then
        echo "${fic}"
    fi
done

是否可以使用 -exec 参数来替换那个丑陋的循环并更快地检索结果?

这可行,但我不知道这是否是最好的方法:

find /home/ouhma -newermt '50 seconds' -type f -exec bash -c 'LANG=C LC_ALL=C grep -Pq "hell\d" <(tail -c 1000 "{}") && echo "{}"' \;

多个 -exec 动作可以一个接着一个,如果前一个 -exec 成功,一个 -exec 将是 运行,即 运行 的命令 -exec returns 退出状态 0.

做:

find /home/ouhma -type f -newermt '50 seconds' -exec env LC_ALL=C \
       bash -c 'grep -Pq "hell\d" <(tail -c 1000 "")' _ {} \; -exec echo {} \;

因为您只是打印文件名,所以这就足够了:

find /home/ouhma -type f -newermt '50 seconds' -exec env LC_ALL=C \
       bash -c 'grep -Pq "hell\d" <(tail -c 1000 "") && echo ""' _ {} \;