将 bash 类型 `${i%.mp3}` 语法与 xargs 一起使用?

发布于 2024-09-26 15:57:56 字数 315 浏览 1 评论 0原文

使用示例(人为的!):将 .mp3 文件重命名为 .txt。

我希望能够做这样的事情,

find -name '*.mp3' -print0 | xargs -0 -I'file' mv file ${file%.mp3}.txt

这不起作用,所以我通过 sed -e 's/.mp3//g 管道 find 的输出',就可以了。

不过看起来有点老套; 有没有办法使用常用的 bash ${x%y} 语法?

Example of usage (contrived!): rename .mp3 files to .txt.

I'd like to be able to do something like

find -name '*.mp3' -print0 | xargs -0 -I'file' mv file ${file%.mp3}.txt

This doesn't work, so I've resorted to piping find's output through sed -e 's/.mp3//g', which does the trick.

Seems a bit hackish though; is there a way to use the usual bash ${x%y} syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

下雨或天晴 2024-10-03 15:57:56

不,xargs -0 -I'file' mv file ${file%.mp3}.txt 将不起作用,因为 file 变量将由 xargs 程序扩展,并且不是外壳。实际上,将其称为变量并不完全正确,在 xargs 手册中将其称为“replace-str”。

更新:要仅使用 xargs 和 bash (而不是 sed 或类似的),您当然可以让 xargs 启动 bash 进程,然后该进程可以执行您想要的任何替换:

find -name '*.mp3' -print0 | xargs -0 bash -c \
'while [ -n "$1" ]; do mv "$1" "${1%.mp3}.txt" ; shift; done;' "bash"

No, xargs -0 -I'file' mv file ${file%.mp3}.txt will not work because the file variable will be expanded by the xargs program and not the shell. Actually it is not quite correct to refer to it as a variable, it is called "replace-str" in the manual of xargs.

Update: To just use xargs and bash (and no sed or similar) you can of course let xargs start a bash process which then can do whatever substitution you want:

find -name '*.mp3' -print0 | xargs -0 bash -c \
'while [ -n "$1" ]; do mv "$1" "${1%.mp3}.txt" ; shift; done;' "bash"
阳光下慵懒的猫 2024-10-03 15:57:56

如果您安装了 GNU Parallel:

find -name '*.mp3' -print0 | parallel -0 mv {} {.}.txt

如果您的文件名不包含 \n(换行符),这也可以:

find -name '*.mp3' | parallel mv {} {.}.txt

观看介绍视频以了解有关 GNU Parallel 的更多信息 http://www.youtube.com/watch?v=OpaiGYxkSuQ

If you have GNU Parallel installed:

find -name '*.mp3' -print0 | parallel -0 mv {} {.}.txt

If your file names do not contain \n (newline) this will work too:

find -name '*.mp3' | parallel mv {} {.}.txt

Watch the intro video to learn more about GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

赏烟花じ飞满天 2024-10-03 15:57:56

只是添加另一个建议,即使它不使用 shell 的替换机制,而是使用 sed 的替换机制。

find -name '*.mp3' -print |
sed -e 's/\.mp3$//' -e "s/.*/mv '&\.mp3' '&\.txt'/" |
sh

换句话说,为每个文件名创建一个 mv 命令行,并将结果传递给 sh

如果任何文件的文件名中包含撇号(单引号),此操作将会失败。我发布这篇文章主要是为了对抗“如果你只有 xargs,那么每个问题都会看起来像钉子”综合症。

并非所有 sed 都具有完全相同的语法。特别是,我不能 100% 确定您可以在一些粗糙的旧 SunOS 上使用多个 -e 参数(在 /usr/ucb/blah/barf/vomit/xpg4/bin 等内容中查找与 XPG 兼容的 sed /sed)

Just to add another suggestion, even though it doesn't use the shell's substitution mechanism, but instead, sed's.

find -name '*.mp3' -print |
sed -e 's/\.mp3$//' -e "s/.*/mv '&\.mp3' '&\.txt'/" |
sh

In other words, create an mv command line for each file name, and pass the result to sh.

This will fail if any of the files have an apostrophe (single quote) in their file name. I mainly post this to counter the "if all you have is xargs, every problem will look like a nail" syndrome.

Not all seds have exactly the same syntax. In particular, I'm not 100% sure you can have multiple -e arguments on some crufty old SunOS (look for the XPG-compatible sed in something like /usr/ucb/blah/barf/vomit/xpg4/bin/sed)

你丑哭了我 2024-10-03 15:57:56
find . -type f -name '*.mp3' | while read -r F
do
  mv "$F" "${F%.mp3}.txt"
done

重击 4+

shopt -s globstar
shopt -s nullglob
for file in **/*.mp3
do
    mv "$file" "${file%.mp3}.txt"
done
find . -type f -name '*.mp3' | while read -r F
do
  mv "$F" "${F%.mp3}.txt"
done

Bash 4+

shopt -s globstar
shopt -s nullglob
for file in **/*.mp3
do
    mv "$file" "${file%.mp3}.txt"
done
牛↙奶布丁 2024-10-03 15:57:56
perl rename.pl 's/\.mp3$/.txt/' *.mp3  
# Or **/*.mp3 for zsh and bash with specific settings set, for a recursive 
# rename.

http://people.sc.fsu.edu/~jburkardt/pl_src /重命名/重命名.html

perl rename.pl 's/\.mp3$/.txt/' *.mp3  
# Or **/*.mp3 for zsh and bash with specific settings set, for a recursive 
# rename.

http://people.sc.fsu.edu/~jburkardt/pl_src/rename/rename.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文