如何使用 Perl 轻松批量重命名文件?

发布于 2024-08-13 03:10:21 字数 440 浏览 6 评论 0原文

我有很多文件正在尝试重命名,我尝试创建一个正则表达式来匹配它们,但即使如此,我仍然卡在文件的命名上,如下所示:

文件名01

文件名100

文件名02

文件名03

等等,我想在任何小于 100 的文件后面添加一个“0”(零),如下所示:

文件名001

文件名100

文件名002

文件名003

我最接近匹配它们的是使用这个 find -type d |排序-r | grep ' [1-9][0-9]$' 但是我不知道如何替换它们。预先感谢您可以为我提供的任何帮助。我使用的是 CentOS,如果这有帮助的话,所有这些都是通过 SSH 完成的。

I have a lot of files I'm trying to rename, I tried to make a regular expression to match them, but even that I got stuck on the files are named like:

File Name 01

File Name 100

File Name 02

File Name 03

etc, I would like to add a "0" (zero), behind any of file that are less than 100, like this:

File Name 001

File Name 100

File Name 002

File Name 003

The closest I got to so much as matching them was using this find -type d | sort -r | grep ' [1-9][0-9]$' however I could not figure out how to replace them. Thanks in advance for any help you can offer me. Im on CentOS if that is of any help, all this is being done via SSH.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

遗失的美好 2024-08-20 03:10:21
perl -e 'foreach $f (glob("File\\ Name*")) { $nf = $f; $nf =~ s/(\d+)$/sprintf("%03d",$1)/e; print `mv \"$f\" \"$nf\"`;}'

也许有点矫枉过正,但它确实满足了要求。

perl -e 'foreach $f (glob("File\\ Name*")) { $nf = $f; $nf =~ s/(\d+)$/sprintf("%03d",$1)/e; print `mv \"$f\" \"$nf\"`;}'

A bit overkill maybe, but it does what is asked.

冰葑 2024-08-20 03:10:21
find . -type d -print0 | xargs -0 rename 's/(\d+)/sprintf "%03d", $1/e' 

或类似的东西,前提是

  1. 您有 GNU find 和 GNU xargs (用于 -print0-0
  2. 您有 perl 附带的“重命名”实用程序
  3. 只有一组文件名中的数字。如果有多个,那么您需要使用正则表达式执行一些操作,使其仅匹配您想要重新格式化的数字。
find . -type d -print0 | xargs -0 rename 's/(\d+)/sprintf "%03d", $1/e' 

or something like that, provided

  1. You have GNU find and GNU xargs (for -print0 and -0)
  2. You have the 'rename' utility that comes with perl
  3. There's only one group of digits in the filename. If there's more than one, then you need to do something with the regex to make it only match the number you want to reformat.
朕就是辣么酷 2024-08-20 03:10:21

这是一次性的事情吗?如果是这样,我将建议一些可能被许多程序员逃避的东西:

通过管道传输命令的输出 (find -type d | sort -r | grep ' [1-9][ 0-9]$')到一个文件,并使用编辑器和一些全局搜索/替换魔法来创建一个执行重命名的脚本。

然后把剧本扔掉。

没有什么大惊小怪的,也很少有可能你会因为尝试一种聪明的(但调试不充分的)单行代码而导致文件中的杂草丛生,最终搬起石头砸自己的脚。

Is this a one-time thing? If so, I'm going to suggest something that might seem to be a cop out by many programmers here:

Pipe the output of your command (find -type d | sort -r | grep ' [1-9][0-9]$') to a file and use an editor along with some global search/replace magic to create a script that does the renames.

Then throw away the script.

There's little fuss and little chance that you'll end up shooting yourself in the foot by having some attempt at a clever (but inadequately debugged) one-liner go off into the weeds on your files.

来世叙缘 2024-08-20 03:10:21

按以下顺序运行两个命令:

$ rename 's/File Name (\d)$/File Name 0$1/' *
$ rename 's/File Name (\d\d)$/File Name 0$1/' *

第一个命令重命名小于 10 的所有内容并在前面添加零。第二个重命名所有小于 100 的内容并在前面添加一个零。所有文件名的结果都应该是三位数。

Run two commands, in this order:

$ rename 's/File Name (\d)$/File Name 0$1/' *
$ rename 's/File Name (\d\d)$/File Name 0$1/' *

First one renames everything less than 10 and prepends a zero. The second one renames everything less than 100 and prepends a zero. The result should be three digits for all filenames.

宛菡 2024-08-20 03:10:21

在我的 debian 中,它可以很好地进行重命名,并使用 300 个文件进行了测试。

 perl -e 'map `touch door$_.txt`, 1..300;'
 rename 's/(\d+)\.txt/sprintf("%03d.txt", $1)/e' *.txt

In my debian it works well with rename, tested with 300 files.

 perl -e 'map `touch door$_.txt`, 1..300;'
 rename 's/(\d+)\.txt/sprintf("%03d.txt", $1)/e' *.txt
剧终人散尽 2024-08-20 03:10:21

我认为 mmv 是你的朋友。

I think mmv is your friend here.

冧九 2024-08-20 03:10:21

你可以使用 perl 或 ruby​​ 做一些事情。

将所有这些文件放在同一目录中

dirlisting = DIR.entries('.')

dirListing.each do |file| 
 num = file.match(/\d+$/).to_i
 if num < 100
   find the position where start the number, with index and inject the 0 there.
 end
end

you could do something using perl or ruby.

put all this files in the same directory

dirlisting = DIR.entries('.')

dirListing.each do |file| 
 num = file.match(/\d+$/).to_i
 if num < 100
   find the position where start the number, with index and inject the 0 there.
 end
end
逆光飞翔i 2024-08-20 03:10:21
use strict;
use File::Copy;

my @files = glob 'File*Name*';

foreach my $filename (@files) {
    if ($filename =~ m`^.*File.*Name.*?(\d+)`) {
        my $number = $1;
        next if ($number > 99);
        rename $filename, sprintf("FileName%03d",$number);
    }
}
use strict;
use File::Copy;

my @files = glob 'File*Name*';

foreach my $filename (@files) {
    if ($filename =~ m`^.*File.*Name.*?(\d+)`) {
        my $number = $1;
        next if ($number > 99);
        rename $filename, sprintf("FileName%03d",$number);
    }
}
撑一把青伞 2024-08-20 03:10:21

如果您的遥控器有 bash shell,

for i in File*; 
do 
    case "${i##* }" in  [0-9][0-9] ) 
      echo  mv "$i" "${i% *} $(printf "%03d" ${i##* })" ;; 
    esac; 
done

请删除“echo”以进行实际重命名

if your remote has bash shell

for i in File*; 
do 
    case "${i##* }" in  [0-9][0-9] ) 
      echo  mv "$i" "${i% *} $(printf "%03d" ${i##* })" ;; 
    esac; 
done

remove "echo" to do actual renaming

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文