[英]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.