deefroud
deefroud

Reputation: 27

Bash command to find files based on a list of filenames

Using terminal on a mac, I have this bash command from my previous research that works great.

It searches only one level of a directory and moves all files (matching a text list of filenames) depositing them into a new directory.

while read filename; do mv DIR/${filename}.jpg NEWDIR/; done < filenamelist.txt

I now need it to able to search through any subfolders within DIR as well. The subfolders will all have random names usually. I don't think the MV move command has any recursive option, and haven't been able to find any other specific solutions. Should I be using a proper shell script instead of just one command line?

I'm still learning, would really appreciate any pointers! Thanks for reading.

Upvotes: 1

Views: 525

Answers (1)

tlo
tlo

Reputation: 1631

You can use find to search in subfolders:

while read filename; do find DIR/ -name "${filename}.jpg" -exec mv "{}" NEWDIR/ \; ; done < filenamelist.txt

You can use the mv options -f to force overwrite or -n to not overwrite existing files.

Upvotes: 1

Related Questions