bash 在 Linux 中重命名多个文件 shell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11108735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:32:05  来源:igfitidea点击:

rename multiple files shell in Linux

linuxfilebash

提问by Linux Rules

I have a number of files such as file_022.bmp, file_023.bmp...file_0680.bmp. I need to rename these to something a little bit more convenient such as file_1.bmp, file_2.bmp...file_658.bmp.

我有许多文件,例如 file_022.bmp、file_023.bmp...file_0680.bmp。我需要将它们重命名为更方便的名称,例如 file_1.bmp、file_2.bmp...file_658.bmp。

Is there a bash script that I could write to do this for me? Thanks for the help and advice.

是否有我可以编写的 bash 脚本来为我执行此操作?感谢您的帮助和建议。

Luke H

卢克·H

回答by Harald Brinkhof

if you're on a debian based linux system then you can use the rename script which accepts regular expressionsto rename files. Some more infobecause I find it hard to find the man page.

如果您使用的是基于 debian 的 linux 系统,那么您可以使用接受正则表达式的重命名脚本来重命名文件。更多信息,因为我发现很难找到手册页。

e.g.

例如

harald@Midians_Gate:~$ ls p*.php

parse.php  pd.php  pgrep.php  preg_based.php  proc.php

suppose I want to change the extension to .perl and prepend the name with file_ then I use command:

假设我想将扩展名更改为 .perl 并在名称前加上 file_ 然后我使用命令:

rename -n 's/([a-z]*)\.php/file_.perl/' p*.php

would give

会给

parse.php renamed as file_parse.perl
pd.php renamed as file_pd.perl
pgrep.php renamed as file_pgrep.perl
preg_based.php renamed as preg_file_based.perl
proc.php renamed as file_proc.perl

I select and capture the base filename ([a-z]*)and then use it in the substitution $1and append .perland prepend $1 with the regular string file_

我选择并捕获基本文件名([a-z]*),然后在替换中使用它,$1并在.perl$1 前面加上常规字符串file_

the -n option makes it test run without changing anything

-n 选项使其在不更改任何内容的情况下测试运行

As you can see from this example your selecting regexp needs to be correctly thought out or you get cases like the above preg_based.php where you wanted file_preg_based.perl :) to compensate for that I would've needed to use ([a-z_]*)here

正如你从这个例子中看到的,你需要正确地考虑选择正则表达式,或者你会得到像上面的 preg_based.php 这样的情况,你想要 file_preg_based.perl :) 来补偿我需要在([a-z_]*)这里使用

It's one of the many reasons why I keep hanging on to debian, I'd love to find the equivalent for other non-debian systems though :-/

这是我一直坚持使用 debian 的众多原因之一,不过,我很想找到其他非 debian 系统的等价物:-/

回答by stelios

if you have files a.bmp,b.bmp,c.bmp and you want to end up with file_1.bmp, file_2.bmp, file_3.bmp

如果你有文件 a.bmp、b.bmp、c.bmp 并且你想以 file_1.bmp、file_2.bmp、file_3.bmp 结束

using bash:

使用 bash:

mkdir result
index=1
for i in *.bmp
do
  mv "$i" "result/file_"$((index++)).bmp
done

notes: using a subdirectory is advised to avoid accidentally overwriting a file that looks like file_xx.bmp

注意:建议使用子目录以避免意外覆盖看起来像 file_xx.bmp 的文件

if you have too many files to fit in the command line after expansion you could use something like:

如果扩展后有太多文件无法放入命令行,则可以使用以下内容:

mkdir result
index=1
find . -name "*.bmp" | while read i
do
  echo mv "$i" "result/file_"$((index++)).bmp
done

after inspecting the output remove the 'echo'

检查输出后删除“回声”