加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – Perl:递归重命名所有文件和目录

发布时间:2020-12-14 01:54:06 所属栏目:Linux 来源:网络整理
导读:我需要递归重命名每个文件和目录.我将空格转换为下划线,并将所有文件/目录名称设置为小写.如何使以下脚本在一次运行中重命名所有文件?目前,在转换所有文件/目录之前,需要多次运行脚本.代码如下: #!/usr/bin/perluse File::Find;$input_file_dir = $ARGV[0]
我需要递归重命名每个文件和目录.我将空格转换为下划线,并将所有文件/目录名称设置为小写.如何使以下脚本在一次运行中重命名所有文件?目前,在转换所有文件/目录之前,需要多次运行脚本.代码如下:

#!/usr/bin/perl

use File::Find;

$input_file_dir = $ARGV[0];

sub process_file {
        $clean_name=lc($_);
        $clean_name=~s/s/_/g;
        rename($_,$clean_name);
        print "file/dir name: $clean_namen";
}
find(&;process_file,$input_file_dir);

解决方法

您需要指定bydepth =>您传递给查找或调用finddepth的选项中的1.从 perldoc File::Find开始:

bydepth

Reports the name of a directory only AFTER all its entries have been reported. Entry point finddepth() is a shortcut for specifying { bydepth => 1 } in the first argument of find().

但是,您仍然需要决定如何处理命名冲突,因为如果目标存在,重命名将破坏目标.

#!/usr/bin/perl

use strict; use warnings;
use File::Find;

finddepth(&;process_file,$_) for @ARGV;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读