这只是一个小脚本,我正在运行以连续循环来检查目录并移动那里的每个文件。此代码有效,我在后台进程中运行它。但由于某种原因,我收到以下错误:'/home/srvc_ibdcoe_pcdev/Niall_Test/new_dir/..' and '/home/srvc_ibdcoe_pcdev/Niall_Test/perl_files/..' are identical (not copied) at move2.pl line 27
任何想法为什么它告诉我它是相同的,即使路径不同?
非常感谢
脚本
#!/usr/bin/perl
use diagnostics;
use strict;
use warnings;
use File::Copy;
my $poll_cycle = 10;
my $dest_dir = "/home/srvc_ibdcoe_pcdev/Niall_Test/perl_files";
while (1) {
sleep $poll_cycle;
my $dirname = '/home/srvc_ibdcoe_pcdev/Niall_Test/new_dir';
opendir my $dh, $dirname
or die "Can't open directory '$dirname' for reading: $!";
my @files = readdir $dh;
closedir $dh;
if ( grep( !/^[.][.]?$/, @files ) > 0 ) {
print "Dir is not empty\n";
foreach my $target (@files) {
# Move file
move("$dirname/$target", "$dest_dir/$target");
}
}
}
答案 0 :(得分:8)
您需要过滤掉..
中的特殊.
和@files
条目。
#!/usr/bin/perl
use diagnostics;
use strict;
use warnings;
use File::Copy;
my $poll_cycle = 10;
my $dest_dir = "/home/srvc_ibdcoe_pcdev/Niall_Test/perl_files";
while (1) {
sleep $poll_cycle;
my $dirname = '/home/srvc_ibdcoe_pcdev/Niall_Test/new_dir';
opendir my $dh, $dirname
or die "Can't open directory '$dirname' for reading: $!";
my @files = grep !/^[.][.]?$/, readdir $dh;
closedir $dh;
if (@files) {
print "Dir is not empty\n";
foreach my $target (@files) {
# Move file
move("$dirname/$target", "$dest_dir/$target");
}
}
}
您看到的消息是正确的。由于..
,两个路径都解析到同一目录;两者都解析为/home/srvc_ibdcoe_pcdev/Niall_Test
..
指的是目录的父目录。