File Duplication Script?

I have a file, let's say 1.jpg, and I have a text file that contains a list of filenames I would like to duplicate 1.jpg as (i.e., 2.jpg, 3.jpg, 4.jpg, etc.). The filenames that I want to create are all on separate lines, one per line.

I'm sure there's a simple solution, but I'm not claiming to be a programmer by any means.

If anybody could help me out I would greatly appreciate it.

Thanks!

if names are in file called "filenames":

for i in `cat filenames`; do cp 1.jpg $i; done

MG

why exactly do you need a cat?

Thank you mglenney! That worked like a charm.

Because most of the time I can figure out how to do something I just don't necessarily know the best way to do it. I know it could also be done like this:

while read i; do cp 1.jpg $i; done < filenames

But I don't know if that's any better. I'm always looking for better (quicker, best practice, etc.) ways of doing things so if you have one I'd appreciate the lesson.

MG

checkout the posted link.

Ok. I was looking at the "Useless use of cat" section at the top but I think you're referring to the section about using backticks. According to that my second example above would be the best route.

Reading that made me think of another question. I created a new thread for it. Maybe you can help me there.

Thanks,

MG

It is far better. Redirection done that way does not require the shell to launch a new process, which is a huge benefit. Also, should filenames happen to be a formidable size, the version using backticks would try to cram the whole thing into one string and cut off most of it, where the loop version can gracefully deal with any list of arbitrary size by reading names one at a time.

And yes, the former does matter even in this day and age. Computers are faster than they used to be, but processes are also bigger and more elaborate than they used to be; launching a new process remains one of the most computationally expensive operations there is, where simple redirection costs nearly nothing next to it.