Shell – How to loop through lines of a file and find files matching each line

findshelltext processingwildcards

In a BASH shell, I would like to take the lines of a file (eg pattern.txt) and find the files on my system whose names contain the patterns in each line of my file. So, I have the following for loop

for pp in `cat pattern.txt`; do find ./ -iname "*${pp}*" -print0; done

which doesn't find any files when if the first line in pattern.txt doe exist. So how can I fix the above command line?

Note: Each line in my file contains characters [a-zA-Z] only.

Best Answer

Try this:

#!/bin/bash

while IFS= read -r pp; do
  find . -iname "*${pp}*" -print0
done < /path/to/pattern.txt

Not sure why you want -print0, but I left it in anyway. Perhaps you are attempting to pipe this to xargs?

Related Question