简体   繁体   English

如何将Perl中的文件列表从一个目录复制到另一个目录

[英]How to copy a list of files in perl from one directory to another

In UNIX get the files from DIRECTORY_PATH based on file format and last modified date and move the file to ARCHIVE_DIRECTORY 在UNIX中,根据文件格式和上次修改日期从DIRECTORY_PATH获取文件,然后将文件移至ARCHIVE_DIRECTORY

For that used the below piece of code 为此使用下面的代码

DIRECTORY_PATH=/apps/data/central_archive/
NO_OF_DAYS_ARCHIVE=10
FILE_FORMAT=txt
EXEC_CMD=mv
ARCHIVE_DIRECTORY=/apps/data/archive/

res=`find $DIRECTORY_PATH -mtime $NO_OF_DAYS_ARCHIVE -name "*$FILE_FORMAT*" -type f|grep "$DIRECTORY_PATH[^/]*$" | grep -v '/rf/'`

Here how do implement the same logic in Perl? 在这里,如何在Perl中实现相同的逻辑?

One way, using File::Find::Rule : 一种方法,使用File :: Find :: Rule

use warnings;
use strict;

use File::Copy;
use File::Find::Rule;

my $dir = 'src/';
my $arch_dir = 'archive/';
my $days = 10;
my $type = '*.txt';

if (! -d $arch_dir){
    mkdir $arch_dir or die $!;
}

my @files = File::Find::Rule->file()
                            ->name($type)
                            ->mtime('> ' . (time() - $days*24*60*60))
                            ->in($dir);

for (@files){
    move $_, $arch_dir or die $!;
    print "moved $_ to $arch_dir/\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件从一个目录复制到另一目录? - How to copy my files from one directory to another directory? 将文件列表从一个位置复制到另一位置 - Copy list of files from one location to another 将文件从一个目录复制到另一个目录而无需替换 - Copy files from one directory to another without replacement Java:将文件从一个目录复制/移动到另一个目录的安全方法 - Java: Safe way to copy/move files from one directory to another 有选择地将文件从一个文件夹目录复制到另一个文件夹 - Selectively copy files from one folder directory to another 将文件从一个目录复制到现有目录 - Copy files from one directory into an existing directory 将多个文件(具有完整路径)从一个目录复制并替换到另一目录的对应路径 - Copy and replace multiple files (with their full path) from one directory to another directory's corresponding paths 如何使用 PHP 将文件从一个目录复制到另一个目录? - How to copy a file from one directory to another using PHP? 将.3gp 文件从一个目录“External Dir”移动或复制到 Android 中的另一个外部目录 - Move or copy .3gp files from one directory “External Dir” to another External directory in Android 从列表中查找一个目录中的文件,将其复制到新目录并执行脚本 - Find files in one directory from a list, copy to a new directory, and execute script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM