0

I'm looking to search and then copy all files with filenames matching in a text file from a directory (with subdirectories) on a server to another directory. I've found a great solution (though quite slow) for windows here.

in a .bat file, something like:

for /f "delims=" %%i in (text-list.txt) do echo D|xcopy "\\SERVER\FOLDER\%%i?" "c:\temp" /i /z /y /s

How can I accomplish this on Mac/Linux?

this works for files that are all in the same directory, but I can't figure out how to search subdirectories within the source folder:

rsync --files-from ~/filelist.txt . ~/destfolder

based on the example from @neofug, this is a perl script to do this. it works well except that it does not search through the subfolders in the srcfolder:

#!/usr/bin/env perl
use strict;
my $textFile = shift @ARGV;
my $filenames = {};
open F1, "<", $textFile or die "Cannot open file $textFile! $!\n";
while ( <F1> ){
  chomp;
  $filenames->{$_}++;
}
close(F1);
my $imgDir = "/Users/username/srcfolder";
chdir($imgDir);
my @imgList = glob "*.txt";
foreach(@imgList){
  if($filenames->{$_}){
    system("/bin/cp $_ /Users/username/destfolder");
  }
}
1

You can us perl to accomplish this, enter the text file list as a command line argument and change the paths to suit your setup:

    #!/usr/bin/env perl
    use strict;
    use File::Find;
    my $textFile = shift @ARGV;
    my $filenames = {};
    open F1, "<", $textFile or die "Cannot open file $textFile! $!\n";
    while ( <F1> ){
      chomp;
      $filenames->{$_}++;
    }
    close(F1);
    my $imgDir = "/home/user/Pictures/";
    my $imgList = {};
    find(\&findPics, "/home/user/Pictures/");
    foreach(keys %$imgList){
      if($filenames->{$_}){
        system("/bin/cp $imgList->{$_} /path/to/newImages/");
      }
    }
    sub findPics{
      if($_ =~ /.+[jpg|jpeg|png|gif]/i){
        $imgList->{$_} = $File::Find::name;
      }
    }

Edit: I modified this program as requested by the op to recurse within the given directory to find all images.

  • Thank you! I haven't used perl before, but that is a great solution. It works for me except that doesn't search through subfolders in the srcfolder. I've added the bit above modified from your example. Any idea on how to make it search recursively? Thanks! – jamierob Apr 25 '16 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.