![使用 perl 脚本“rename”将文件从 in1.jpg、in2.jpg、in3.jpg 等重命名为 out0001.jpg、out0002.jpg、out0003.jpg 等](https://linux22.com/image/1388734/%E4%BD%BF%E7%94%A8%20perl%20%E8%84%9A%E6%9C%AC%E2%80%9Crename%E2%80%9D%E5%B0%86%E6%96%87%E4%BB%B6%E4%BB%8E%20in1.jpg%E3%80%81in2.jpg%E3%80%81in3.jpg%20%E7%AD%89%E9%87%8D%E5%91%BD%E5%90%8D%E4%B8%BA%20out0001.jpg%E3%80%81out0002.jpg%E3%80%81out0003.jpg%20%E7%AD%89.png)
我想将大量名为 in1.jpg、in2.jpg、in3.jpg 等的文件重命名为 out0001.jpg、out0002.jpg、out0003.jpg 等。如果有人能建议使用 perl 脚本rename
(或prename
)作为解决方案,我将不胜感激,该脚本是 perl 发行版附带的。
答案1
只是为了好玩而且免费......
我会使用链接来保留原始名称,但如果您愿意,可以将链接命令(ln
)更改为移动命令(mv
)(添加为评论):
#!/usr/bin/perl
# Creates link files with ascending sequence numbers ($USE_SEQ=1) or original numbers
# ($USE_SEQ=0):
$USE_SEQ = 1;
unless(opendir(DIRECTORY,".")) { die "Unable to open current directory.\n"; }
@files = readdir(DIRECTORY);
closedir(DIRECTORY);
$seqnum = 1;
foreach $file (@files) {
if ($file =~ /\.jpg$/i) {
($number) = ($file =~ /(\d+).jpg/i);
$number = $seqnum++ if $USE_SEQ;
$padded_number = sprintf "%05d", $number;
$padded_name = "out" . $padded_number . ".jpg";
`ln -s "$file" $padded_name`;
# `mv "$file" $padded_name`; # untested
}
}