How can i find all filenames that match a string in a list inside a file?

I have a list_file.txt with a list of email addresses each in a new line like this:


In the same directory i have many files that their filenames contains one of the email addresses of the list like this: email_1@mail.com.txt, email_2@mail.com.txt, etc.

¿How can i copy all matching files to a new directory?

for i in $(cat list_file.txt) ; do
  cp "email_$i.txt" newdirectory/

or if you want each match to be copied to its own separate directory:

for i in $(cat list_file.txt) ; do
  mkdir -p email_$i
  cp "email_$i.txt" "email_$i/"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.