Copy files to a dir using from a list

Hi all,
I'd very grateful for some help with the following:

I have a directory with several subdirectories with files in them. All files are named different, even between different subdirectories. I also have a list with some of those file names in a txt file (without the path, just the file name), like:

a.jpg
d.jpg
f.jpg
...

I would like to copy all the files in the file list to a different directory without copying all of them, but I can't figure out how to. I know how to use find to locate any file path and copy it, but don't know how to make it for a list of them.

Thanks,
foracoffee

find /path/to/root > allfiles

awk -F"/" 'NR==FNR '{ A[$1]++; next } $NF in A' filelist allfiles | while read LINE
do
        echo cp "$LINE" /path/to/dest
done

rm allfiles
1 Like

there are no duplicates, all names are different

---------- Post updated at 06:40 PM ---------- Previous update was at 06:16 PM ----------

when I try to execute the awk part, it shows a > sign each time I push 'enter'. I've been looking and it seems I have to create a file with the awk code? Sorry, haven't used it before

I misplaced a quote, sorry.

The awk code doesn't create a file, the find code does.

find /path/to/root > allfiles

awk -F"/" 'NR==FNR { A[$1]++; next } $NF in A' filelist allfiles | while read LINE
do
        echo cp "$LINE" /path/to/dest
done

rm allfiles
1 Like

Thanks! It works now!

However, it only makes:

cp . /results

being '/results' my path/to/dest. I've looked at the filelist and the allfiles an they have files in common but don't show as:

cp file /results

any idea what I might be doing wrong?

---------- Post updated at 07:25 PM ---------- Previous update was at 07:14 PM ----------

It works! Some of the files in the list still have some path, when I removed, it worked fine :slight_smile:

Thanks a lot!

1 Like

Ah. That could also have been remedied with

awk -F"/" 'NR==FNR '{ A[$NF]++; next } $NF in A'
1 Like