Copy file after searching in a directory

Hi,

I am looking for an answer for following senario:

I have a text file (base.txt) which consist list of files to be searched like:
base.txt

abc.txt
def.txt
fgh.txt

Now i am going to search all the listed files in another directory after reading them one by one, once i found the file in the directory then i want to copy it to another location.

Please suggest how i can fullfill this scenario.

Thanks all for your valuable suggestion.

 
cat base.txt | while read file
do
   cp <location1>/$file <location2>/$file 2>/dev/null
done

why exactly do you need a cat?

To read list of files to be searched/copied in base.txt. No?

What vgersh99 is getting at:

while read file
do
   cp <location1>/$file <location2>/$file 2>/dev/null
done < base.txt

(no cat)

I see.. That's right. cat should be avoided. Tks !!

I wouldn't go that far! Personally, I think this whole UUOC thing is over-stated. It would be better described as an "unnecessary use of cat" :slight_smile:

On slower machines a while back, writing scripts that open files with cat and then piping the ouptut into a program that could have opened the file all by itself got dubbed 'UUOC'.

It creates an extra process. On machines with more limited resources UUOC was discouraged, and still is, as a waste of system resources. An extension of that, creating way more child processes than need inside a loop is still a major performance issue.

http://http://partmaps.org/era/unix/award.html

Using anything unnecessarily should be discouraged, but to describe it as "useless" is overstating the mark.

It's not useless because it works. But it is unnecessary.

thanks buddies... it worked fine with both cat and "no cat". ..thanks a lot...

Is loop also required as well? How about this.

cd location1
cp `cat /path_if_any/base.txt|xargs` location2/