我有这个find命令,使所有文件在最后50秒内被修改,这与最后1000个字符中的以下regex hell\d匹配。我使用尾部来获得最后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 "{}"' \;发布于 2016-10-20 08:24:54
多个-exec操作可以相互跟踪,如果前面的-exec成功运行,即-exec运行的命令返回退出状态0,则将运行一个-exec。
做:
find /home/ouhma -type f -newermt '50 seconds' -exec env LC_ALL=C \
bash -c 'grep -Pq "hell\d" <(tail -c 1000 "$1")' _ {} \; -exec echo {} \;由于您只是在打印文件名,这就足够了:
find /home/ouhma -type f -newermt '50 seconds' -exec env LC_ALL=C \
bash -c 'grep -Pq "hell\d" <(tail -c 1000 "$1") && echo "$1"' _ {} \;https://stackoverflow.com/questions/40148764
复制相似问题