bash 中的全局重命名

发布于 2024-11-05 04:30:34 字数 533 浏览 8 评论 0原文

我对 bash 还很陌生,如果这是一个基本问题,我很抱歉。我试图重命名一堆 mp3 文件以在其文件名前添加 1- ,但遗憾的是 mv *.mp3 1-*.mp3 不起作用。所以我尝试编写它的脚本,首先使用 echo 来测试命令:

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done

这似乎输出了我喜欢的命令,所以我删除了 echo,将命令更改为

for f in *.mp3 ; do mv \'$f\' \'1-$f\'; done

Which failed。接下来,我尝试像这样继续传递命令,

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done | /bin/sh

哪个有效,但如果有人能启发我为什么中间命令不起作用,我将有兴趣知道。或者,如果有一种更优雅的单行可以满足我的要求,我也有兴趣看看。

I'm fairly new to bash so sorry if this is kind of a basic question. I was trying to rename a bunch of mp3 files to prepend 1- to their filenames, and mv *.mp3 1-*.mp3 didn't work unfortunately. So I tried to script it, first with echo to test the commands:

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done

Which seems to output the commands that I like, so I removed the echo, changing the command to

for f in *.mp3 ; do mv \'$f\' \'1-$f\'; done

Which failed. Next I tried piping the commands onward like so

for f in *.mp3 ; do echo mv \'$f\' \'1-$f\'; done | /bin/sh

Which worked, but if anyone could enlighten me as to why the middle command doesn't work I would be interested to know. Or if there is an more elegant one-liner that would do what I wanted, I would be interested to see that too.

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

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

发布评论

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

评论(2

琴流音 2024-11-12 04:30:34

我认为你必须将命令更改为

for f in *.mp3 ; do mv "$f" "1-$f"; done

否则你会将 'file1.mp3''1-file1.mp3' 之类的内容传递给 mv(包括单引号)。

I think you have to change the command to

for f in *.mp3 ; do mv "$f" "1-$f"; done

Otherwise you would pass something like 'file1.mp3' and '1-file1.mp3' to mv (including the single quotes).

回眸一遍 2024-11-12 04:30:34

试运行:

rename -n 's/^/1-/' *.mp3

如果运行命令看起来正确,请删除 -nman rename 了解详细信息。

Dry run:

rename -n 's/^/1-/' *.mp3

Remove the -n if it looks correct to run the command. man rename for details.

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