简体   繁体   English

Linux重命名文件夹名为ab1.jpg,ab2.jpg,ab10.jpg等的文件

[英]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.

相关问题 在Linux或MacOS X中将文件20141207_190822.jpg重命名为“ 2014-12-07 19.08.22.jpg” - Renaming files like 20141207_190822.jpg to “2014-12-07 19.08.22.jpg” in linux or MacOS X 在Linux上将jpg从文件夹复制到文件夹 - Copy jpg from folder to folder on Linux 是否可以将目录中的所有文件重命名为0.jpg,1.jpg,2.jpg等? - Is it possible rename all files in a directory to 0.jpg, 1.jpg, 2.jpg, etc? Linux:在csv中递归“ ls”所有jpg文件 - Linux: 'ls' all jpg files recursively in csv Linux将now.jpg.1重命名为spy_1.html - Linux renaming now.jpg.1 to spy_1.html 使用ab进行网站基准测试 - Website Benchmarking using ab 如何使用 Linux 命令将“1.jpg”等文件夹的所有文件重命名为“1 hello.jpg”? - How can I rename all file of a folder like '1.jpg' to '1 hello.jpg' with Linux command? 如何使用linux命令将jpg文件转换为png文件? + 难度 = 子文件夹 - How to convert jpg files into png files with linux command? + Difficulty = Subfolders 如何在Linux上将pptx文件转换为jpg或png(对于每张幻灯片)? - How to convert pptx files to jpg or png (for each slide) on linux? 二进制*的操作数无效(有'ab {aka struct a}'和'ab * {aka struct a *}') - Invalid operands to binary * (have ‘ab {aka struct a}’ and ‘ab * {aka struct a *}’)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM