Linux search subdirectories and among .js .py files for a specific keyword

Advertisements

I am trying to search for the file/script with a specific keyword in all the sub-directories beginning from the root or home directory. My search yielded so many files, but I want to search only .js, .py types. I want to know the file name containing this matching word.

grep -name '*.js' -rl "matching word" ./

present output:

grep: invalid max count

>Solution :

Here is one way:

find start_dir -type f \( -name "*.js" -o -name "*.py" \) -exec grep -l "word" {} \;

It will find all .js or .py files in or under the start directory and then grep them for the given word. There’s other ways, but this is my "go to" for this type of thing.

Leave a Reply Cancel reply