user65457
user65457

Reputation: 247

How can I copy a directory but ignore some files in Perl?

In my Perl code, I need to copy a directory from one location to another on the same host excluding some files/patterns (e.g. *.log, ./myDir/abc.cl). What would be the optimum way of doing this in Perl across all the platforms? On Windows, xcopy is one such solution. On unix platforms, is there a way to do this in Perl?

Upvotes: 3

Views: 1460

Answers (3)

brian d foy
brian d foy

Reputation: 132876

I think you're looking for rsync. It's not Perl, but it's going to work a lot better than anything you make in Perl:

% rsync --exclude='*.log' --exclude='./myDir/abc.cl' SOURCE DEST

If you have a bunch of patterns, you can put those all in a file:

*.log
./myDir/abc.cl

Now ignore all the patterns in a file:

% rsync --exclude-from=do_not_sync.txt SOURCE DEST

Upvotes: 5

ghostdog74
ghostdog74

Reputation: 342669

On *nix, you can use native tar command, with -exclude options. Then after creating the tar file, you can bring it over to your destination to untar it.

Upvotes: 1

Nate
Nate

Reputation: 484

I'd use File::Find, and step over each file, but instead of calling File::Copy's copy() on each file, first test to see if it matches the pattern, and then next if it does.

Upvotes: 2

Related Questions