如何批量重命名具有编号计数的文件夹中的40000个文件 [英] How to batch rename 40000 files on a folder with number count
问题描述
我需要对一个文件夹中的40000个文件进行批处理重命名,其末尾的数字是这样的: something.jpg到something_00001.jpg.我想使用重命名命令,但是任何有效的方法都可以.有帮助吗?谢谢!
I need to batch rename 40000 files on a folder with a number count in the end like this:. something.jpg to something_00001.jpg. I'd like to work with the rename command, but anything that works will do. Any Help? Thanks!
推荐答案
这些功能强大的命令将非常快速地进行大量更改-请测试一小部分数据的副本.
These are powerful commands that will make lots of changes very rapidly - please test on a copy of a small subset of your data.
方法1-使用重命名"(Perl工具)
这应该与rename
一起使用,您可以通过以下方式将其安装在macOS上:
This should work with rename
which you can install on macOS with:
brew install rename
命令为:
rename --dry-run -N "00001" 's/.jpg$/_$N.jpg/' *jpg
删除--dry-run
以便实际执行命令,而不仅仅是告诉您它将执行的操作.
Remove the --dry-run
to actually execute the command rather than just tell you what it would do.
方法2-使用GNU并行
或者,它应该与 GNU Parallel 一起使用,您可以通过以下方式将其安装在macOS上:
Alternatively, this should work with GNU Parallel which you can install on macOS with:
brew install parallel
命令为:
find . -name \*.jpg -print0 | parallel -0 --dry-run mv {} {.}_{#}.jpg
其中:
-
{}
表示当前文件" , -
{.}
表示当前文件减去扩展名" ,并且 -
{#}
表示(顺序)作业编号"
{}
means "the current file",{.}
means "the current file minus extension", and{#}
means "the (sequential) job number"
删除--dry-run
以便实际执行命令,而不仅仅是告诉您它将执行的操作.
Remove the --dry-run
to actually execute the command rather than just tell you what it would do.
您提到您想要一个偏移量,因此以下内容的偏移量为3:
You mentioned you wanted an offset, so the following works with an offset of 3:
find . -name \*.jpg -print0 | parallel -0 'printf -v new "%s_%05d.jpg" "{.}" $(({#}+3)); echo mv "{}" "$new"'
方法3-无需其他软件
这只能使用标准的内置工具来工作:
This should work using just standard, built-in tools:
#!/bin/bash
for f in *.jpg; do
# Figure out new name
printf -v new "%s_%05d.jpg" "${f%.jpg}" $((cnt+=1))
echo Would rename \"$f\" as \"$new\"
#mv "$f" "$new"
done
删除倒数第二行中的#
即可进行重命名.
Remove the #
in the penultimate line to actually do the rename.
方法4-无需其他软件
这应该仅使用标准的内置工具即可工作.请注意,macOS附带了Perl,并且它应该更快,因为它不会像以前的方法那样为40,000个文件中的每个文件启动新的mv
进程.相反,Perl仅启动一次,它读取find
传递给它的以空值结尾的文件名,然后为每个文件执行库调用:
This should work using just standard, built-in tools. Note that macOS comes with Perl installed, and this should be faster as it doesn't start a new mv
process for each of 40,000 files like the previous method. Instead, Perl is started just once, it reads the null-terminated filenames passed to it by find
and then executes a library call for each:
find . -name \*.jpg -print0 | perl -0 -e 'while(<>){ ($sthg=$_)=~s/.jpg//; $new=sprintf("%s_%05d.jpg",$sthg,++$cnt); printf("Rename $_ as $new\n"); }'
如果看起来正确,请更改最后一行,以便实际上进行重命名,而不只是告诉您它将做什么:
If that looks correct, change the last line so it actually does the rename rather than just telling you what it would do:
find . -name \*.jpg -print0 | perl -0 -e 'while(<>){ ($sthg=$_)=~s/.jpg//; $new=sprintf("%s_%05d.jpg",$sthg,++$cnt); mv $_, $new; }'
这篇关于如何批量重命名具有编号计数的文件夹中的40000个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!