bash 中的全局重命名
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你必须将命令更改为
否则你会将
'file1.mp3'
和'1-file1.mp3'
之类的内容传递给mv
(包括单引号)。I think you have to change the command to
Otherwise you would pass something like
'file1.mp3'
and'1-file1.mp3'
tomv
(including the single quotes).试运行:
如果运行命令看起来正确,请删除
-n
。man rename
了解详细信息。Dry run:
Remove the
-n
if it looks correct to run the command.man rename
for details.