Find files per names in input file and copy them to output folder

Hi Experts,

My requirement is to find files recursively using names given in input file(names_list.txt) and copy them all to output folder with name parent_foldername_filename.xml.

Example:
File List

./a/test1.xml
./a/test2.xml
./b/test1.xml
./b/test3.xml

Input file content

test1.xml
test3.xml

after command is run output folder ./output should have following

./output/a_test1.xml
./output/b_test1.xml
./output/b_test3.xml

Thanks for your help!

so far I am able to find files by name "find . -name *.xml" but could not upgrade this to my requirement.

Please specify your operating system and shell details.

This is using GNU find on Linux systems.

In my dummy working dir i have :

$ ls -lrt
total 20
-rw-r--r-- 1 user user   20 Jul  2 20:26 input.txt
drwxr-xr-x 2 user user 4096 Jul  2 20:26 a
drwxr-xr-x 2 user user 4096 Jul  2 20:26 b
drwxr-xr-x 2 user user 4096 Jul  2 20:55 output
-rwx------ 1 user user  210 Jul  2 20:58 fn.sh

find against xml files producing :

$ find . -type f -name '*.xml'
./a/test2.xml
./a/test1.xml
./b/test1.xml
./b/test3.xml

fn.sh script containing :

find . -type f -name '*.xml' -printf "%h %f\n" | while read FLOC FL
do
awk -v floc="$FLOC" -v fl="$FL" ' \
{ d=split(floc,f,"/"); if ( fl == $0 ) print "cp " floc"/"fl, "output/"f[d]"_"fl } ' \
input.txt
done

fn.sh producing when ran in working directory :

$ ./fn.sh
cp ./a/test1.xml output/a_test1.xml
cp ./b/test1.xml output/b_test1.xml
cp ./b/test3.xml output/b_test3.xml

Is this the end result you are expecting ?

Hope that helps
Regards
Peasant.

yes , thank you.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.