`{} {}some` vs `{} {,some}` 在 `find` 命令的 `-exec` 选项中的区别

huangapple go评论47阅读模式
英文:

`{} {}some` vs`{} {,some}` in the `-exec` option of the `find` command

问题

这是一种使用find命令和-exec选项来在目录中将特定后缀的文件重命名的方法。两个命令的作用相同,只是语法不同。

第一个命令:

find . -name '*01' -exec mv {} {}bar \;

这个命令使用-exec选项来执行mv命令来重命名文件。{}表示find命令找到的每个文件的占位符。在这个命令中,{}会被替换为文件名,然后再加上bar后缀,从而实现重命名。

第二个命令:

find . -name '*01' -exec mv {}{,bar} \;

这个命令也使用-exec选项执行mv命令,但是它使用了{}{,bar}的语法。这里的{}表示文件名,而{,bar}表示将文件名与bar连接在一起,实现了相同的重命名效果。

两种语法都可以用于重命名文件,只是第二个命令使用了更紧凑的语法来实现相同的目标。

英文:

I want to add some files in the directory with some suffix. I usually do this:

find . -name '*01' -exec mv {} {}bar \;

But recently I came across another version of this command that does the same thing:

find . -name '*01' -exec mv {}{,bar} \;

Why does this work? What is this syntax?

答案1

得分: 1

这个语法是花括号扩展。但是...

更好的用法:

find . -name '*01' -exec bash -c 'mv "$1" "$1.bar"' bash {} \;

或者更好的方法,使用Perl的rename

shopt -s globstar # 启用递归查找 **

rename 's/$/.bar/' **/*01
英文:

This syntax is brace expansion. But...

Better use:

find . -name '*01' -exec bash -c 'mv "$1" "$1.bar"' bash {} \;

or even better, using Perl's rename:

shopt -s globstar # enable recursion with **

rename 's/$/.bar/' **/*01

huangapple
  • 本文由 发表于 2023年5月8日 01:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定