Multiple keyword search in multiple directories bash script

I guess the title is good enough :)
Probably you can do it in single grep command too.. but this way you will play with bash as well..

here is the bash script doing it


 #! /bin/bash
 dirs=(dir1 dir2 dir3 dir4 dir5)
 keywords=(keyword1 keyword2 keyword3 keyword4 keyword5)
 strs=""
 for word in "${keywords[@]}"; do
     strs+=" -e ${word} "
 done
 
 for path in "${dirs[@]}"; do
     dirname="$(basename "${path}")"
     grep -ir --color ${strs} ${path}
 done

Save this as finder.sh

and you can run it as bash finder.sh

See more on bash here

Regular expression for file and directory listing

I guess ls is a laudable command in the Linux world – the fact that everything is a file, we exploit it in a daily manner.
One usage of the a helpful command on ls is grep for listing regex’d listing.
here is the the simplest command that can list all the files/directories with that start with ‘pe’
First we would be on the directory we want the listing, then we would pipe the listing to grep!

ls | grep "^pe.*"

Even

ls | grep "^pe" 

would result the same.
One that contains pe would be

ls | grep "pe"

and the one that ends with pe would be

ls | grep "pe$"

Further reading Ubunutu help on grep