替换文件夹名称中的文本

发布于 2024-08-29 12:15:44 字数 566 浏览 3 评论 0原文

如何替换 Linux 中文件夹名称中的相同文本?

假设我有“Photos_Jun”、“Photos_July”、“Photos_Aug”等,我可以将它们重命名为“Photos Jun”、“Photos July”等的最简单方法是什么(基本上我想用空格替换下划线“”我有大约 200 个这样的文件夹,

我正在寻找解决方案:如何使用 Perl 轻松批量重命名文件?

它看起来像我正在寻找的东西,但是,我不知道如何制作正则表达式来匹配字母数字后跟“_”的文件夹

。 -数字名称,所以我认为 [a-zA-Z] 是正确的开始方式,

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

感谢您的帮助!

How can I replace the same text in folder names in linux?

Say I have "Photos_Jun", "Photos_July", "Photos_Aug", etc. whats the simplest way I can rename them like "Photos Jun", "Photos July", etc (basically I want to replace the underscore with a space " ". I have about 200 of these folders.

I was looking at solution: How can I easily bulk rename files with Perl?

It looks like what im looking for however, I dont know how to make a regular expression to match folders that are alphanumeric followed by a "_".

All files have non-numeric names, so I think [a-zA-Z] is the right way to start.

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

Thanks for any help!

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

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

发布评论

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

评论(3

回眸一遍 2024-09-05 12:15:44

Linux 有一个 rename 命令:

rename '-' ' ' Photos_*

Linux has a rename command:

rename '-' ' ' Photos_*
或十年 2024-09-05 12:15:44

如果您使用 *nix 并且不介意非 Perl 解决方案,这里有一个 shell (bash) 解决方案。满意后删除echo

#!/bin/bash
shopt -s extglob
for file in +([a-zA-Z])*_+([a-zA-Z])/; do echo mv "$file" "${file//_/ }"; done

if you are on *nix and you don't mind a non Perl solution, here's a shell (bash) solution. remove the echo when satisfied.

#!/bin/bash
shopt -s extglob
for file in +([a-zA-Z])*_+([a-zA-Z])/; do echo mv "$file" "${file//_/ }"; done
睫毛溺水了 2024-09-05 12:15:44
perl -e 'use File::Copy; foreach my $f (glob("*")) { next unless -d $f; my $nf = $f; $nf =~ s/_/ /g; move($f, $nf) || die "Can not move $f to $nf\n"; }

展开单行:

use strict; # Always do that in Perl. Keeps typoes away.
use File::Copy; # Always use native Perl libraries instead of system calls like `mv`
foreach my $f (glob("*")) {
    next unless -d $f; # Skip non-folders
    next unless $f =~ /^[a-z_ ]+$/i; # Reject names that aren't "a-zA-Z", _ or space
    my $new_f = $f; 
    $new_f =~ s/_/ /g; # Replace underscore with space everywhere in string
    move($f, $nf) || die "Can not move $f to $nf: $!\n";
                     # Always check return value from move, report error
}
perl -e 'use File::Copy; foreach my $f (glob("*")) { next unless -d $f; my $nf = $f; $nf =~ s/_/ /g; move($f, $nf) || die "Can not move $f to $nf\n"; }

Tu unroll the one-liner:

use strict; # Always do that in Perl. Keeps typoes away.
use File::Copy; # Always use native Perl libraries instead of system calls like `mv`
foreach my $f (glob("*")) {
    next unless -d $f; # Skip non-folders
    next unless $f =~ /^[a-z_ ]+$/i; # Reject names that aren't "a-zA-Z", _ or space
    my $new_f = $f; 
    $new_f =~ s/_/ /g; # Replace underscore with space everywhere in string
    move($f, $nf) || die "Can not move $f to $nf: $!\n";
                     # Always check return value from move, report error
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文