【问题标题】:"Copy failed: File too large" error in perlperl 中的“复制失败:文件太大”错误
【发布时间】:2010-07-12 19:04:57
【问题描述】:

好的,我的文件夹中有 650 万张图像,我需要尽快将它们移动。我会将它们移到它们自己的文件夹结构中,但首先我必须将它们移出此服务器。

我尝试了 rsync 和 cp 以及各种其他工具,但它们最终总是出错。所以我写了一个 perl 脚本以更直接的方法提取信息。使用 opendir 并让它计算所有文件非常完美。它可以在大约 10 秒内将它们全部数完。现在我尝试将我的脚本再提高一个档次,让它实际移动文件,我得到错误“文件太大”。这一定是某种错误错误,因为文件本身都很小。

#!/usr/bin/perl
#############################################
# CopyFilesLite
# Russell Perkins
# 7/12/2010
#
# Tool is used to copy millions of files
# while using as little memory as possible. 
#############################################

use strict;
use warnings;
use File::Copy;

#dir1, dir2 passed from command line
my $dir1 = shift;
my $dir2 = shift;
#Varibles to keep count of things
my $count = 0;
my $cnt_FileExsists = 0;
my $cnt_FileCopied = 0;

#simple error checking and validation
die "Usage: $0 directory1 directory2\n" unless defined $dir2;
die "Not a directory: $dir1\n" unless -d $dir1;
die "Not a directory: $dir2\n" unless -d $dir2;

opendir DIR, "$dir1" or die "Could not open $dir1: $!\n";
while (my $file = readdir DIR){
  if (-e $dir2 .'/' . $file){
   #print $file . " exsists in " . $dir2 . "\n"; #debuging 
   $cnt_FileExsists++;
  }else{
   copy($dir1 . '/' . $file,$dir2 . '/' . $file) or die "Copy failed: $!";
   $cnt_FileCopied++;
   #print $file . " does not exsists in " . $dir2 . "\n"; #debuging 
  }
  $count++;
}
closedir DIR;

#ToDo: Clean up output. 
print "Total files: $count\nFiles not copied: $cnt_FileExsists\nFiles Copied: $cnt_FileCopied\n\n";

那么你们中有人遇到过这种情况吗?什么会导致这种情况以及如何解决?

【问题讨论】:

    标签: linux perl copy opendir


    【解决方案1】:

    关于您的错误处理代码,能否请将or die "Copy failed: $!"; 更改为 'or die "Copy failed: '$dir1/$file' to '$dir2/$file': $!";' ?

    然后它应该告诉你错误发生在哪里。

    然后检查两件事 -

    1) 每次都在同一个文件上失败吗?

    2) 那个文件有什么特别之处吗?奇怪的名字?不寻常的大小?不是普通文件?根本不是文件(正如另一个答案所推测的那样)?

    【讨论】:

      【解决方案2】:

      我不确定这是否与您的问题有关,但readdir 将返回所有目录内容的列表,包括子目录(如果存在)以及许多操作上的当前 (.) 和父目录 (..)系统。您可能正在尝试复制目录和文件。 以下不会尝试复制任何目录:

      while (my $file = readdir DIR){
          next if -d "$dir1/$file";
      

      【讨论】:

      • 它确实看到了 .和 .. 但由于它们位于两个位置,因此会跳过它们。它只复制 dir1 而不是 dir2 中的文件
      【解决方案3】:

      这似乎是我挂载到它的服务器的 nfs 挂载的问题。我连接了一个 USB 驱动器,文件正在以极快的速度复制……如果你把 USB 2 算作极端的话。

      【讨论】:

      • nfs 对目录中的最大文件数有硬性限制。
      【解决方案4】:

      一个文件夹中的 650 万张图像非常极端,并且仅仅为了读取一个目录就给机器带来了负担,无论是在 shell 中还是在 Perl 中。这是一个很大的文件夹结构。

      我知道您现在正在寻找 Perl 中的解决方案,但是当您从 shell 处理这么多文件时,您需要利用 xargs 命令。通过将文件分组为可管理的块,它可以提供很大帮助。 http://en.wikipedia.org/wiki/Xargs

      【讨论】:

        【解决方案5】:

        可能你发送数据的分区的文件系统不支持非常大的数据。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-08
          • 2019-08-28
          • 1970-01-01
          • 1970-01-01
          • 2014-03-02
          • 2016-08-03
          相关资源
          最近更新 更多