How to read file containing a string and finding it in a directory

63 views Asked by At

I have a file with a list of file names. I want to find each of those files and copy it to some directory, is this possible in linux?

ListOfFileNames.txt
xyz.txt
ags.txt
shd.txt
...

Directory_to_be searched
dsf.txt
xyz.txt
shd.txt
...

Empty_new_directory

So copy xyz.txt, ags.txt, shd.txt and place them in Empty_new_directory

Any help will be appreciated

xargs cp -t /app/dest/ < ListOfFileNames.txt

Does not work?

Maybe use of find command

1

There are 1 answers

0
Jakub Roztocil On BEST ANSWER

If there's no nesting, then you can use a simple loop with cp:

SOURCE='Directory_to_be_searched'
TARGET='Empty_new_directory'

cat File.txt |  while read filename; do
    cp "${SOURCE}/${filename}" "${TARGET}/${filename}"  
done