0

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:

1@mail.com
2@mail.com
3@mail.com
...

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?

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

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/"
done

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.