So I have one file, filters.txt that contains names like these:


And then there's a directory, New/ with a lot of files named like this:


So I'm basically looking for a Unix way to copy all the files in New/ that contain one of the patterns in filters.txt

I could write a Python script, but I can't really think of an easy way to do this with a shell script, if someone knows of a way I'd like to see it

  • What have you actually tried so far? Anything?
    – bu5hman
    Commented Nov 12, 2019 at 19:32
  • Wrote a Python script that does it but it's about 12 lines of code. I couldn't really think of a shell script one liner that does it so was wondering if someone else could figure one out as a learning experience for me Commented Nov 12, 2019 at 19:38
  • Hey I wrote a bash script which may also help those on here. github.com/jordyjwilliams/copy_filenames_from_txt Commented Oct 23, 2022 at 17:48

2 Answers 2


Quick and dirty...

<filters.txt xargs -i% bash -c "eval cp *%* New/"
  • Yep this works, thank you. I'll add it to my toolbelt Commented Nov 12, 2019 at 19:39
  • 1
    Safer with xargs -I {} sh -c 'cp *"$1"* New/' sh {} since wonky filenames ($(reboot)) would not cause havoc.
    – Kusalananda
    Commented Oct 23, 2022 at 22:13

This may be an option:

for f in *.csv; do 
  [[ -n $(echo $f | grep -f filters.txt) ]] && cp $f dest/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .