Solved

copying files using perl/unix

Posted on 2011-03-07
7
377 Views
Last Modified: 2012-05-11
Hi,
 I want to copy a file from source to destination by matching the directory names.

Example:   Batch0001  is the higher level directory and 4567890R is the sub directory which consists of several  log files.
Source:                                                                                                                  
Batch0001 -> 4567890R
                 ->  1237896R
                 ->   4561089R
Batch0002  ->  ....
......


Destination:
Batch0001 -> 4567890R
                 ->  1237896R
                 ->   4561089R
Batch0002  ->  ....
......

My source and destination will have the same directory structure and filenames. So it has to compare them and when it encounters same  directory and sub directory it has to copy from  source to des a file by name :  4567890R(subdirectory name)_nornalize.log
0
Comment
Question by:new_perl_user
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:Martin_J_Parker
ID: 35057319
If you just want to copy all of the *_normalize.log files across from /source to /dest on Linux and the directory structures already match between /source and /dest then all you should need to do is:

cd /source
find  .  -name  '*_normalize.log'  -exec  cp  {}  /dest/{}  ';'

and if you want it to list the files as it is copying use:

cd /source
find  .  -name  '*_normalize.log'  -exec  cp  {}  /dest/{}  ';'  -print
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 250 total points
ID: 35058731
What if the destination directory doesn't exist?  Should it be created or should those files not be copied?

Change the variable values where indicated in the comments and this should do what you want.  If it doesn't work, let me know what is wrong and I'll fix it.
#!/usr/local/bin/perl

use strict;
use warnings;
use File::Copy qw(cp);
use File::Find;
use File::Path;

use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

# change these to the real source and destination root folders
my $src_dir = '/src';
my $dest_dir = '/dest';

# change this to 1 if you want to make the dest dir if it does not exist
# 0 will skip the file if the dest dir does not exist
my $mk_dest = 0;

use Cwd ();
my $cwd = Cwd::cwd();

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, $src);
exit;

sub wanted {
    return unless /^_normalize.log$/;
    my $file = $_;
    my $dpath = $dir;
    # remove $src_dir from the dir
    $dpath =~ s{^$src_dir}{$dest_dir};
    if ($mk_dest) {
        make_path($dpath) unless (-d $dpath);
    } else {
        return unless (-d $dpath);
    }
    cp $name, "$dpath/$file" or die "could not copy $name: $!";
}

Open in new window

0
 
LVL 10

Assisted Solution

by:Martin_J_Parker
Martin_J_Parker earned 250 total points
ID: 35058936
If the destination directory does not exist you can still do something similar with a simple bash script
- just change /source and /dest to the source and destination directory names:

#!/bin/bash
#
#Start in the source directory
cd /source
#
#Find all the normalize.log files:
for i in `find . -name *_normalize.log`; do
#
# For each file find what it's directory name is relative to /source
   j=`dirname $i`
#
# If the directory does not exist in /dest then create it
   if [ ! -d /dest/$j ]
   then
      mkdir /dest/$j
   fi
#
#Copy the file over to /dest
   cp $i /dest/$i    
done
#
#End of bash script

You can make the script executable with the command
chmod 755 scriptname
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:point_pleasant
ID: 35083808
Very simple one liner that will create the directories as needed.  If you need the original dates of the files use the -m option with cpio

find /source -name *_normalize.log -depth | cpio -pmdv /dest
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35084183
If his system has cpio (most but not all do).

Also, won't that command copy the files to /dest/src/path/*_normalize.log (rather than /dest/path/*_normalize.log)?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35084215
One minor tweak to my code - line 30 should be:

return unless /_normalize.log$/;

Otherwise, it will only look for files named (exactly) _normalize.log.
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35084394
possibly depends on the version, but if it does simply cd into source directory first then execute.

find . -name *_normalize.log -depth | cpio -pmdv /dest
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now