bash - 为什么带有后缀的基本名称与 find 一起使用时在子 shell 中不起作用?

标签 bash find subshell command-substitution

下面的命令没有进行替换,为什么?

find ./ -name "*.dng" -exec echo `basename \{\} .dng`  \;

但是这个命令有效:

find ./ -name "*.dng" -exec basename \{\} .dng  \;

我实际上想做的是找到硬盘中的所有 dng 并执行以下操作:

touch -c -r {basename}.RW2  {basename}.dng

最佳答案

The following command doesn't do the subtitution, why?

find ./ -name "*.dng" -exec echo `basename \{\} .dng`  \;

正如赛勒斯在 his comment 中所说的那样, bash 在调用 find 之前将 `basename\{\} .dng` 扩展为 {};所以 find 接收到的只是 echo {},它看不到 `basename\{\} .dng` 部分。

What I'm actually trying to do is to find all the dng in my hard drive and do:

touch -c -r {basename}.RW2 {basename}.dng

假设每个引用文件 (*.RW2) 与相应的 .dng 文件位于同一目录中,我会这样做:

find . -name '*.dng' -exec sh -c '
for dng do
  touch -c -r "${dng%.*}.RW2" "$dng"
done' _ {} +

关于bash - 为什么带有后缀的基本名称与 find 一起使用时在子 shell 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62355444/

相关文章:

linux - inotifywait 命令不检测文件但检测文件夹

Python - 找到相似的颜色,最好的方法

Python 子进程、子 shell 和重定向

tee - tee 的退出状态>(...)

bash - 如何在 bash 命令替换中使用 `set -e`?

linux - 将多个用户批量分配到多个组

linux - 当我执行 "rm -rf data"时,删除顺序是什么?

linux - 如何删除字符串输出的前两个单词

bash - find 命令中的 %p 和 %P 有什么区别

find - 防止 bash 脚本的参数通配