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
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 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:
grep -inr --include \*.txt "foo" ~/folder
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:
grep -inr --include \*.html --include \*.css --include \*.js "foo" ~/folder
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:
grep -inr --exclude \*.txt "foo" ~/folder
There’s also a flag to exclude entire directories at once:
grep -inr --exclude-dir config "foo" ~/folder
Using find Instead
Alternatively, if you prefer using the
find utility to search through files, you can connect it to
grep using pipes and
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.
find ./ -type f -iname "*.txt" -print0 | xargs -0 grep "foo"