Perl命令将一个目录下所有文件夹中的文件移动到一个目标文件夹中

#!/usr/bin/perl

use strict;
use warnings;

my $dir = '/path/to/directory';   # 替换成实际目录路径
my $dest_dir = '/path/to/destination';  # 替换成实际目标目录路径

opendir(DIR, $dir) or die "Could not open directory: $!\n";

while (my $sub_dir = readdir(DIR)) {
    next if ($sub_dir =~ /^\.{1,2}$/); # 跳过当前目录和上级目录
    next unless (-d "$dir/$sub_dir"); # 只处理子目录,跳过文件
    opendir(SUB_DIR, "$dir/$sub_dir") or die "Could not open subdir: $!\n";
    while (my $file = readdir(SUB_DIR)) { # 遍历子目录中的文件
        next if ($file =~ /^\.{1,2}$/);  # 跳过当前目录和上级目录
        rename("$dir/$sub_dir/$file", "$dest_dir/$file") or die "Could not move file: $!\n"; # 将文件移动到目标目录中
    }
    closedir(SUB_DIR);
}

closedir(DIR);
 

/path/to/directory替换成实际目录路径,将/path/to/destination替换为实际目标目录路径,然后保存为脚本文件(例如move_files.pl)并在终端中执行此脚本即可。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值