You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GNU find中-exec与-printf的输出顺序是否有明确保障?

GNU find中-exec与-printf的执行顺序确认

核心结论

GNU find严格按照表达式书写的左到右顺序执行带副作用的谓词,不会对-exec、-printf这类操作重排序,因此输出顺序完全可预期:

  • 对于命令 find <paths> -exec cmd '{}' \; -printf <format>:每个匹配文件会先执行-exec输出一行,紧接着执行-printf输出一行,循环交替,绝不会出现批量-exec输出后再批量输出-printf的情况。
  • 对于反向顺序命令 find <paths> -printf <format> -exec cmd '{}' \;:每个匹配文件会先输出-printf的内容,再执行-exec输出,同样是严格的交替顺序,不会打乱批量输出。

依据与验证

GNU find的表达式评估规则明确:仅对无副作用的测试类谓词(如-name、-size)做有限优化调整顺序,而-exec(执行外部命令)、-printf(输出内容)这类带有明确副作用的操作,必然严格按书写顺序执行。

实际验证示例:
假设当前目录有file1、file2、file3三个文件,执行命令:

find . -type f -exec echo "exec: {}" \; -printf "printf: %f\n"

输出必然为:

exec: ./file1
printf: file1
exec: ./file2
printf: file2
exec: ./file3
printf: file3

反向顺序命令:

find . -type f -printf "printf: %f\n" -exec echo "exec: {}" \;

输出则为:

printf: file1
exec: ./file1
printf: file2
exec: ./file2
printf: file3
exec: ./file3

内容的提问来源于stack exchange,提问作者user19087

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:48:12