0

好的,所以我在一个文件夹中有 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";

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

4

5 回答 5

1

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

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

然后检查两件事 -

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

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

于 2010-07-12T20:49:02.050 回答
0

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

while (my $file = readdir DIR){
    next if -d "$dir1/$file";
于 2010-07-12T19:19:52.033 回答
0

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

于 2010-07-12T21:02:28.873 回答
0

也许您发送数据的分区文件系统不支持非常大的数据。

于 2010-07-13T03:54:55.863 回答
0

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

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

于 2010-07-13T03:23:10.827 回答