我想将大量名为 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
}
}