[英]Linux Renaming files in a folder where the names is ab1.jpg, ab2.jpg, ab10.jpg etc
The order of the files is determined by a number that can be embedded in the filename, but sometimes in the beginning of the name eg file1.txt file2.txt file3.txt file10.txt file11.txt etc.. or 1.txt 2.txt 10.txt etc.. 文件的顺序由可以嵌入文件名中的数字确定,但有时在文件名的开头,例如file1.txt file2.txt file3.txt file10.txt file11.txt等。或1.txt 2 .txt 10.txt等。
The renaming should result in names like... file01.txt file02.txt file03.txt file10.txt etc... 重命名的名称应类似于... file01.txt file02.txt file03.txt file10.txt等...
It is important that file1.txt will be file01.txt and not file10.txt to be file01.txt. 重要的是file1.txt将是file01.txt,而不是file10.txt将是file01.txt。
I think the filenames have to be formatted before renaming. 我认为文件名必须在重命名之前进行格式化。 I have no idea of how to do that on the command line, maybe it must be done by a script but I hope not.
我不知道如何在命令行上执行此操作,也许它必须通过脚本来完成,但我希望不要这样做。
The command should be given the number of digits we should have in the final name. 应该给命令指定最终名称中应该包含的位数。 If its possible to use a formatting string we also could give the position where we have the number(s).
如果可以使用格式字符串,我们也可以给出我们拥有数字的位置。
Using the perl rename
utility: 使用perl
rename
实用程序:
rename -n 's/\d+/sprintf("%02d", $&)/e' *.txt
Result would be: 结果将是:
$ ls
file10.txt file1.txt file2.txt file3.txt
$ rename -n 's/\d+/sprintf("%02d", $&)/e' *.txt
rename(file1.txt, file01.txt)
rename(file2.txt, file02.txt)
rename(file3.txt, file03.txt)
If that looks good, remove the -n
dry-run flag. 如果看起来不错,请除去
-n
dry-run标志。
Note that the format string to sprintf
determines the "width" of the zero-padding, so if you were dealing with filenames that get into the triple digits, you'd want to change that to "%03d"
, etc.. 请注意,
sprintf
的格式字符串确定了零填充的“宽度”,因此,如果要处理的文件名变为三位数,则需要将其更改为"%03d"
,等等。
$ ls
file100.txt file10.txt file1.txt file2.txt file3.txt
$ rename -n 's/\d+/sprintf("%03d", $&)/e' *.txt
rename(file10.txt, file010.txt)
rename(file1.txt, file001.txt)
rename(file2.txt, file002.txt)
rename(file3.txt, file003.txt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.