Nicole Stein
Nicole Stein

Reputation: 925

grep for multiple filenames in a directory

I have a file (let's call it images.txt) - on each line is an image path. ie.

"images/dashboard/unit4A/lesson1.png"
"images/dashboard/unit4A/lesson2.png"
"images/dashboard/unit4A/lesson3.png"
"images/dashboard/unit4A/lesson4.png"
"images/dashboard/unit4A/lesson5.png"

etc.

I want to grep an entire directory (app) to see if each image path is mentioned anywhere in the HTML, CSS, or JS files in that directory.

I tried grep -F -f images.txt app/* but it complains that app/* has subdirectories, and also, it doesn't do exactly what I want (I don't need the actual lines that the images are found - I just need to know, for each image, whether it is present or not)

So a sample output might look like:

"images/dashboard/unit4A/lesson1.png" - found
"images/dashboard/unit4A/lesson2.png" - not found
"images/dashboard/unit4A/lesson3.png" - found
"images/dashboard/unit4A/lesson4.png" - not found
"images/dashboard/unit4A/lesson5.png" - found

Just returning a list of images that were not found would also be acceptable.

Is there a good way to do this using grep?

Upvotes: 1

Views: 165

Answers (1)

anubhava
anubhava

Reputation: 786291

You can use xargs with recursive grep like this:

xargs -I % grep -ilR '%' /app/ --include={*.html,*.css,*.js} < images.txt

EDIT Based on edited question you can do:

while read -r pat; do
   printf "%s - " "$pat"
   grep -iqR "$pat" /app/ --include={*.html,*.css,*.js} && echo "found" || echo "not found"
done < images.txt

Upvotes: 2

Related Questions