Operating System - HP-UX
1837179 Members
2398 Online
110113 Solutions
New Discussion

Re: Command to copy range of files.

 
Gulam Mohiuddin
Regular Advisor

Command to copy range of files.

I am copying files with the following command which works fine.

cp fsprod_arch_70{36,37,38,39,40,41,42,43}.log /arch01/fsprod

I need to mention range of values like [36-43]but the follwing commands are not working:

cp fsprod_arch_70{36-43}.log /arch01/fsprod
cp fsprod_arch_70[36-43].log /arch01/fsprod

Any idea how to specify ranage of values?

Thanks,

Gulam.
Everyday Learning.
7 REPLIES 7
Pete Randall
Outstanding Contributor

Re: Command to copy range of files.

You would need to specify it like this:

cp sprod_arch_70[3-4][6-3].log /arch01/fsprod


Pete

Pete
Pete Randall
Outstanding Contributor

Re: Command to copy range of files.

I'll take that back, the [6-3] won't work so you'll have to specify [3-4][67890123]


Pete

Pete
john korterman
Honored Contributor

Re: Command to copy range of files.

Hi,

on a single line you probably have to something like this:

# cp fsprod_arch_703[6-9].log fsprod_arch_704[0-6].log /arch01/fsprod

regards,
John K.
it would be nice if you always got a second chance
Jeff_Traigle
Honored Contributor

Re: Command to copy range of files.

Actually, Pete's recommendation won't get you what you want. You'll be getting 30 through 49 instead of 36 through 43. Only way to do this dynamically I can think of is to use a loop:

LOW=36
HIGH=43
INDEX=${LOW}

while [ ${INDEX} -le ${HIGH} ]
do
FILELIST="${FILELIST} fsprod_arch_70${INDEX}.log"
done

cp ${FILELIST} /arch01/fsprod
--
Jeff Traigle
A. Clay Stephenson
Acclaimed Contributor

Re: Command to copy range of files.


I think this while loop may be executing for a rather long time:

while [ ${INDEX} -le ${HIGH} ]
do
FILELIST="${FILELIST} fsprod_arch_70${INDEX}.log"
done

It might benefit from an increment of ${INDEX} somewhere.

If it ain't broke, I can fix that.
Jeff_Traigle
Honored Contributor

Re: Command to copy range of files.

Yeah, that is very true. This should do much better.

LOW=36
HIGH=43
INDEX=${LOW}

while [ ${INDEX} -le ${HIGH} ]
do
FILELIST="${FILELIST} fsprod_arch_70${INDEX}.log"
INDEX=$((${INDEX}+1))
done

cp ${FILELIST} /arch01/fsprod

That'll teach me to type in a hurry. (Well, ok, probably not.) :)
--
Jeff Traigle
James R. Ferguson
Acclaimed Contributor

Re: Command to copy range of files.

Hi Gulam:

If we are going to loop and increment, it's faster to use integer arithmetic by declaring our variables with 'typeset'. Too, we only need a LOW and a HIGH variable.

#!/usr/bin/sh
typeset -i LOW=36
typeset -i HIGH=43
while (( LOW <= HIGH ))
do
cp fsprod_arch_70${LOW}.log /arch01/fsprod
(( LOW=${LOW}+1 ))
done
exit 0

Regards!

...JRF...