There are a few things you can do to use grep recursively within certain file extensions. The first is to use the -R flag to specify that you want to search for all occurrences of a specific word or phrase. For example, if you want to find all files with the word “password” in their name, you would use the following command: grep -R “password” *.txt The second thing you can do is use the -i flag to specify that you want to find all files with a specific type of information in them. For example, if you want to find all files with JPEG images in their name, you would use the following command: grep -i JPEG *.jpg The third thing you can do is use the -v flag to specify that you want to verbosely report every line of output from your grep operation. For example, if your grep operation produces only a single line of output, you would use the following command: grep -v
grep is a great tool for searching through files and standard input in Linux and is able to match string and Regex patterns. However, sometimes it’s necessary to control what kinds of files grep searches for, and it has flags built in to do just that.
Only Including Certain Files in grep Searches
By default, grep will search all files in a given folder and its subfolders if you invoke it with the recursive -r flag. This will pick up everything, but if you only want certain extensions, the option you’ll want to use is –include.
The –include flag tells grep to only include files matching a certain pattern. If it’s specified, grep will treat all include flags as a whitelist. You can use this with any Linux glob characters, such as wildcards to match everything including a certain extension:
Note that this is escaped with a backslash \ because it’s possible for filenames to have asterisks in them. You can also specify multiple –include flags, for example, searching all HTML, JS, and CSS source files in a wwwroot:
You can similarly also exclude certain file names, which will still match everything except for the glob, acting as a blacklist on top of the existing configuration:
There’s also a flag to exclude entire directories at once:
Using find Instead
Alternatively, if you prefer using the find utility to search through files, you can connect it to grep using pipes and xargs. find can do searching with patterns and Regex, and has a number of advantages, including being able to filter files easily based on metadata like size, date created and modified, and other Linux identifiers.
The command is a little obtuse, as you’ll need to use -print0 at the end of find to print out a single line list, and then pass it to xargs -0 and grep from there.