Perl:将文件从一个位置复制到另一个位置

Fro*_*man 5 perl move

这只是一个小脚本,我正在运行以连续循环来检查目录并移动那里的每个文件.此代码有效,我在后台进程中运行它.但由于某种原因,我收到以下错误:'/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");

    }
}

}
Run Code Online (Sandbox Code Playgroud)

too*_*lic 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");

    }
}

}
Run Code Online (Sandbox Code Playgroud)

您看到的消息是正确的.两个路径都解析到同一目录,因为..; 两人都决心/home/srvc_ibdcoe_pcdev/Niall_Test

.. 指的是目录的父目录.