1833590 Members
4021 Online
110061 Solutions
New Discussion

cp many files

 
SOLVED
Go to solution
Robie Lutsey
Frequent Advisor

cp many files

Hey everyone! I have a simple (to you) question.

I have a directory that has about 83000 images in it and I want to break it up. The files are named LV100000, LV100001 etc.. How do I copy a range of files? i.e. LV100000-LV129000.
11 REPLIES 11
Hai Nguyen_1
Honored Contributor
Solution

Re: cp many files

You can try this for your range LV100000-LV129000:

# cp -p LV1[0-2][0-9]000 full_path_to_new_dir

I believe the syntax is correct but you may run into an error like "... argument list too long".

Hai
Paul Sperry
Honored Contributor

Re: cp many files

Cliff,

You could write a simple script.
--------------------------
# /usr/sbin/ksh
x=99
while [ $x != 129 ]
do
let x=x+1
cp LV$x000 /
done
---------------------------
where is the destination you want to copy the files to

Make the file Executable and run it. ./script

This is a very basic script and you can modify it to do many other tasks.

Have fun

And oh yea, as always say hi to Ken.
F. X. de Montgolfier
Valued Contributor

Re: cp many files

Hi,



The easiest way would of course to split it in LV1*, LV2* and so on, but you may need to use finer regular expressions:

in a regular expression, [0-X] will contain all digits between 0 and X.
For instance, foo[0-3]bar will be true for foo0bar, foo1bar,foo2bar and foo3bar.

In the example that you gave, one command will not be enough:

cp LV1[0-2][0-8]* newdir will copy from LV100000 to LV128999.
cp LV129000 newdir will be mandatory to copy the last file...

if you have too many files, you may not be able to do it this way (too many arguments)...

Hope this helps,

FiX
Paul Sperry
Honored Contributor

Re: cp many files

Cliff,

You do have alot of files to copy. I think this is the script you want to use. I miss understood your file naming convention the first time around.
----------------------------
# /usr/sbin/ksh
x=99999
while [ $x != 129000 ]
do
let x=x+1
cp LV$x /
done
----------------------------

Don't forget to say hi to Ken
;-)
Ian Lochray
Respected Contributor

Re: cp many files

typeset -Z6 startnum=1234
typeset -Z6 stopnum=1256
while [[ ${stopnum} -ge ${startnum} ]]
do
cp LV${startnum} /dest
(( startnum = ${startnum} + 1 ))
done

You just need to amend the values of startnum and stopnum.
Robie Lutsey
Frequent Advisor

Re: cp many files

When I use the typeset command is it going to rename my files as well? My box doesn't have a man page for it. (I'll google it after I post).

Paul, Ken says email him. :)

Thanks for all these answers guys!
Darren Prior
Honored Contributor

Re: cp many files

Hi,

To avoid the "argument too long" error I'd be tempted to use the find command; though obviously running a large number of copies isn't great for performance!

Assuming you're in the dir with the image files:

find . -name 'LV1[0-2][0-9]000' -exec cp {} /dir_to_copy_to \;

should do the job.

regards,

Darren.
Calm down. It's only ones and zeros...
Ian Lochray
Respected Contributor

Re: cp many files

Clifford,
the typeset command is just going to pad the number with zeroes on the left hand side to make sure that the number is six digits.
To read about typeset do a "man ksh".
Robie Lutsey
Frequent Advisor

Re: cp many files

I lied!! The files are named LV100000.jpg. How do I handle the extention? I tried working with the Left Justify and it doesn't work. Thanks!
Robie Lutsey
Frequent Advisor

Re: cp many files

Neeever mind... I figured it out.
Chris Vail
Honored Contributor

Re: cp many files

I note your later post where you say that the files have slightly different names. I have found the cp command to be bloody slow. The tape command tar is a lot faster. Here's how to copy the first 100 files.
tar cvf - LV0000[0-9][0-9]|(cd DESTINATION;tar xvf -)

Copy 500 files: use LV000[0-4][0-9][0-9] for the first argument. Then to get the next 500, use LV000[5-9][0-9][0-9].
LV00[0-4][0-9][0-9][0-9] gets the first 5000 files.

And so on....
Try it: it goes a lot faster.

Chris