当前位置: 首页 > news >正文

find命令的-exec参数的特殊语法{} +和{} \

man find 可以直接看

🧩{} +是什么?

这是find命令的-exec参数的特殊语法,不是通用的 shell 语法。

对比:

写法行为举例
-exec command {} \;每个匹配的文件执行一次commandcp file1 /target/
cp file2 /target/
-exec command {} +将所有匹配的文件一次性传给commandcp file1 file2 file3 ... /target/

{}是文件占位符,+告诉find:收集所有文件,并在最后替换掉{},一次性执行命令。

优点:启动次数少,效率高(尤其当文件很多时)。


-exec command {} + This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of `{}' is allowed within the com‐ mand, and it must appear at the end, immediately before the `+'; it needs to be escaped (with a `\') or quoted to protect it from interpretation by the shell. The command is executed in the starting directory. If any invocation with the `+' form returns a non-zero value as exit status, then find returns a non-zero exit status. If find encounters an error, this can sometimes cause an immediate exit, so some pending commands may not be run at all. For this reason -exec my-com‐ mand ... {} + -quit may not result in my-command actually being run. This variant of -exec always returns true.

🧠cp -t是什么?

cp命令的正常语法是cp 源文件 目标,但当源文件有多个时,目标必须在最后

例如:

cpfile1 file2 file3 /target/

但是find ... -exec cp {} +会把所有文件放在{}的位置,这相当于:

cpfile1 file2 file3... /target/

这本身是合法的,但问题是:/target/必须写在所有文件之后,而find ... -exec cp {} +默认只能把{}放在最后。

也就是说,你不能写成:

find...-execcp/target/{}+# 错误,{} 必须在最后

为了解决这个问题,cp提供了-t选项(--target-directory),允许你先指定目标目录

cp-t/target/ file1 file2 file3...

于是,find ... -exec cp -t /target/ {} +就完美符合{} +的要求:目标在{}之前,所有文件在{}处扩展,一次性复制所有文件。


❌ 为什么不用-d

  • cp -d是用来保留符号链接属性的选项(--no-dereference --preserve=links),与指定目标目录无关。
  • 不存在cp -d /target/这种用法
选项作用
-t指定目标目录(最常用在批量复制)
-d复制符号链接本身,而非指向的文件
-r递归复制目录

✅ 总结

问题答案
{} +是特殊用法吗?是,find-exec的优化语法,表示批量传参
为什么cp-t不是-d因为-t用于指定目标目录,-d含义完全不同
为什么不能直接cp {} /target/+因为{} +要求{}在命令的最后,而普通cp需要目标在最后,两者冲突,因此借助-t解决

所以,find ... -exec cp -t /target/ {} +是一个标准且高效的模式,专门用于把find找到的大量文件一次性复制到指定目录。


简单翻译手册

别担心,这段手册确实写得很技术化。我帮你用通俗的语言拆解一下。


🎯 核心意思(一句话版)

-exec command {} +的作用是:find找到的所有文件,像串糖葫芦一样串起来,一次性传给command命令执行,而不是找到一个执行一次。


📖 逐句翻译 + 解释

1. “This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end”

翻译:这个-exec的变体会在选中的文件上执行指定的命令,但命令行的构建方式是把每个文件名追加到命令的末尾

人话:假设find找到了a.txtb.txtc.txt,那么实际执行的命令是:

commanda.txt b.txt c.txt

注意:command要自己处理这些参数。


2. “the total number of invocations of the command will be much less than the number of matched files”

翻译:命令被调用的总次数会远少于匹配到的文件数。

人话:找到 1000 个文件,不用执行 1000 次命令,可能只执行几次(甚至一次)。效率高。


3. “The command line is built in much the same way that xargs builds its command lines”

翻译:命令行的构建方式和xargs类似。

人话:你可以把-exec {} +理解为find内置了一个xargs


4. “Only one instance of{}' is allowed within the command, and it must appear at the end, immediately before the+'”

翻译:命令里只能有一个{},而且它必须出现在最后,紧挨着+的前面。

人话{}只能放一次,必须是... {} +这种形式,不能有cp {} /target/ +

这也是为什么我们之前用cp -t /target/ {} +,因为cp -t把目标目录提前了,让{}可以在最后。


5. “it needs to be escaped (with a\) or quoted to protect it from interpretation by the shell”

翻译{}需要转义(加\)或加引号,以防止被 shell 解释。

人话:在 shell 里直接写{}没问题,但在某些环境下可能需要写成'{}'\{\}


6. “The command is executed in the starting directory”

翻译:命令会在find启动时的目录中执行。

人话:你在/home/user执行find . -name "*.txt" -exec ...,那么...里的命令也在/home/user执行,而不是在找到文件的那个子目录。


7. “If any invocation with the `+’ form returns a non-zero value as exit status, then find returns a non-zero exit status.”

翻译:如果某次调用(批处理的那次)返回非零退出状态,那么find也会返回非零。

人话:如果命令执行失败了(比如cp报错),find也会告诉系统“我这边出问题了”。


8. “If find encounters an error, this can sometimes cause an immediate exit, so some pending commands may not be run at all.”

翻译:如果find遇到错误(比如权限问题),可能会立即退出,导致某些已经收集好的命令根本没被执行。

人话:极端情况下,find可能在还没执行{} +的命令时就退出了。


9. “For this reason -exec mycommand … {} + -quit may not result in mycommand actually being run”

翻译:因此,-quit-exec ... {} +一起用可能根本不会执行命令。

人话-quit会让find提前退出,所以后面的-exec可能永远跑不到。


10. “This variant of -exec always returns true”

翻译:这个变体的-exec永远返回“真”。

人话:无论command执行成功还是失败,find都会继续处理剩下的文件(不会因为某个命令失败而停止)。


✅ 总结:你不需要全看懂

你只需要记住:

你想要的效果写法
找到一个执行一次-exec command {} \;
找到一批执行一次(高效)-exec command {} +
命令的目标目录需要提前(如cpcommand -t /target/ {} +
避免目录被匹配-type f

至于手册里的细节,大部分你一辈子都不会用到。

http://www.jsqmd.com/news/850936/

相关文章:

  • 从老式万用表到精密测量:双积分ADC如何用‘慢’换来‘准’?选型避坑指南
  • 2026 年珠三角广东等地区飞机盒五大品牌排名及解析,全方位解析各品牌核心竞争力与市场布局逻辑 - 十大品牌榜
  • 2026年EB-5移民公司哪家好?行业机构选择指南 - 品牌排行榜
  • 工业控制新方案:电容HMI与字符LCD组合应用实战
  • 重构屏幕翻译体验:Translumo如何重新定义实时语言转换
  • 从查重到降 AIGC,2026 年 9 款论文工具横评:Paperxie 领衔,谁才是本科生的 “熬夜救星”?
  • 沈阳周边两日自驾游有哪些值得探访的小众目的地 - 资讯焦点
  • 中山黄金吊坠回收同城白银回收同城铂金回收钻石首饰回收本地贵金属回收本地排名正规门店专业推荐哪家靠谱二手哪家强 - 检测回收中心
  • 京大大回收:专业卡券回收首选 - 资讯焦点
  • 5分钟打造整洁桌面:NoFences开源桌面整理工具完全指南
  • 怎么将5v电升到12v?
  • 在Hermes Agent中自定义Provider接入Taotoken服务
  • HashMap与ConcurrentHashMap实现原理详解
  • 一份给公建业主的自动门厂家挑选指南 - 速递信息
  • 天津黄金手镯回收纯银回收白金回收50分钻石回收二手钻石回收本地排名正规门店专业推荐哪家靠谱二手哪家强 - 检测回收中心
  • 中山万足金回收银戒指回收铂金戒指回收碎钻回收奢侈品首饰回收高价多少钱一克同城价格查询上门上门估价闲置变现转让靠谱权威排行榜 - 检测回收中心
  • 益阳旧黄金回收旧银饰回收PT950铂金回收钻戒回收金银铂钻回收高价多少钱一克同城价格查询上门上门估价闲置变现转让靠谱权威排行榜 - 检测回收中心
  • 杭州避暑亲子好去处:OMG 心跳乐园,溶洞避暑 + 亲子玩乐一站式搞定 - 博客湾
  • 洞悉车辆本源 —— 车况查询平台筑牢二手车行业发展基石 - 品牌评测官
  • 2026护发素测评指南:解决分叉修护难题,适配全发质日常护理 - 资讯焦点
  • 手把手教你搞定Windows下的NAMD和VMD安装(附最新版下载与注册避坑指南)
  • SPT-AKI存档编辑器:打破《逃离塔科夫》单机版成长壁垒的专业工具
  • 银川黄金吊坠回收同城白银回收同城铂金回收钻石首饰回收本地贵金属回收本地排名正规门店专业推荐哪家靠谱二手哪家强 - 检测回收中心
  • 告别卫生间反复渗漏返修 防水维修行业企业选购指南 - 资讯焦点
  • 面试官:Zookeeper 的典型应用场景有哪些?你能说上来几个?
  • 天津黄金项链回收老银器回收旧铂金回收1克拉钻石回收二手铂金回收本地排名正规门店专业推荐哪家靠谱二手哪家强 - 检测回收中心
  • 开源学习005——上传一个完整的项目
  • 中山足金回收银手镯回收PT990铂金回收钻石戒指回收旧首饰回收高价多少钱一克同城价格查询上门上门估价闲置变现转让靠谱权威排行榜 - 检测回收中心
  • WINCC flexible在西门子全集成自动化中的核心价值与工程实践
  • 鹰潭金条回收银条回收铂金项链回收克拉钻石回收婚嫁首饰回收高价多少钱一克同城价格查询上门上门估价闲置变现转让靠谱权威排行榜 - 检测回收中心