Operating System - HP-UX
1753379 Members
5409 Online
108792 Solutions
New Discussion юеВ

Copying files for different instance names

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Copying files for different instance names

Hi all,

I need to copy a log file from /logs/{instance-name}/abc.log-08222009 to /tmp/

As "abc.log-08222009" name is same for all the instances it overwrites after copying to /tmp/ directory.

I want to append the instance name from source and assign it to destination file name.

Say for example:

cp /logs/venus/abc.log-08222009 /tmp/abc.log-08222009-venus

cp /logs/moon/abc.log-08222009 /tmp/abc.log-08222009-moon

As there are hundred of different instance names i want to copy using single command to save time.

Can someone suggest proper command or script ?

Thanks,
Shiv
8 REPLIES 8
Steven E. Protter
Exalted Contributor
Solution

Re: Copying files for different instance names

Shalom,

I can suggest a structure.

Make a file list and then process it.


ls -1 > list

while read -r fn
do
cp $fn /tmp/instance
done < list


You can expand on this.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Copying files for different instance names

Hi Shiv:

This is trivial with Perl.

# cat ./mycopy
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
my ( $oldname, $newname );
while (<>) {
chomp( $oldname = $_ );
( $newname = $oldname ) =~ s{/logs/(.+)/(.+)}{/tmp/$2-$1};
copy( $oldname, $newname ) or warn "Can't copy '$oldname': $!\n";
}
1;

...

You can feed the "old" filenames (those to be copied) from a pipe or as an input file:

# find /path -type f -name "abc.log*" | ./mycopy

(or):

# ./mycopy file_of_list_of_files

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Copying files for different instance names

Is single command possible with the combination of awk or sed ?
James R. Ferguson
Acclaimed Contributor

Re: Copying files for different instance names

Hi (again) Shiv:

> Is single command possible with the combination of awk or sed ?

How long a command line do you want? How obtuse do you want to make it? What will you do everytime you want to use it? Why do you ask for 'awk' or 'sed' --- you could manage parts of your objective in either since 'awk' has a 'system()' function and 'sed' can do regular expression backreferences but neither can do both like Perl.

HENCE, do you want a solution or do you want to tell us the only way you want your problem solved?

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Copying files for different instance names

Sorry James for misunderstanding!! I am interested in solutions.
I was just exploring if it was possible and not rigid on a specific solutions.
James R. Ferguson
Acclaimed Contributor

Re: Copying files for different instance names

Hi (again) Shiv:

> Sorry James for misunderstanding!! I am interested in solutions.
I was just exploring if it was possible and not rigid on a specific solutions.

Then please accept my apology for mis-reading your query! Sometimes of late, you never know. I am a believer in TMTOWTDI and I should have taken your question in that context.

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Copying files for different instance names

No Problem.
Your help and contribution has benefitted me lot on this forum.
This forum keeps HP-UX ahead of others.
Dennis Handly
Acclaimed Contributor

Re: Copying files for different instance names

>I want to append the instance name from source and assign it to destination file name.
cp /logs/venus/abc.log-08222009 /tmp/abc.log-08222009-venus

For this exact pattern you can use:
for file in /logs/*/abc.log*; do
new_file=$(echo $file | sed -e 's:/logs/\([^/]*\)/\(.*\):\2-\1:')
cp $file /tmp/$new_file
done