Solved

copying files using perl/unix

Posted on 2011-03-07
7
367 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

12 Experts available now in Live!

Get 1:1 Help Now