1. Overview

Having the ability to scan the contents of documents for a specific string of text is an invaluable skill for any Linux enthusiast to have. Taking it one step further, being able to specify a file type when searching through all of the files in a directory would help shorten search time and help identify desired content quicker.

In this tutorial, we’ll explore the grep command. This command is great for quickly searching through files for a specific string of text.

We’ll be using three test files with the .cc, .h, and .txt file extensions. There is also another folder named Test with the same three files located inside:

$ ls -R
.:
 Test
 test.cc
 test.h
 test.txt
./Test:
 test.cc
 test.h
 test.txt

The syntax of grep needs to follow grep [OPTIONS…] PATTERNS [FILE…] where the PATTERN is what the text in the FILE must match:

$ grep "Hello" *
grep: Test: Is a directory
test.cc:Hello World!
test.h:Hello World!
test.txt:Hello World!

This command runs grep with no options, the pattern “Hello,” and runs it on every file in the local directory.

With files that match the PATTERN, it will output the file’s name, followed by the line that contains the matching text.

When the grep command comes across a directory, it will state the directory’s name followed by “Is a directory”. This can be seen above with the Test directory.

Many other options could be used with grep. For example, if we used the -i and -r options, we could specify text insensitivity as well as setting grep to search recursively:

$ grep -r -i "hello" *
Test/test.txt:Hello World!
Test/test.h:Hello World!
Test/test.cc:Hello World!
test.cc:Hello World!
test.h:Hello World!
test.txt:Hello World!

3. Specifying File Type

Sometimes we may need to limit the results to specific file types that we may be working with. To do so, we can use a special type of option that starts with two hyphens:

$ grep -ri "hello" --include=*.cc
Test/test.cc:Hello World!
test.cc:Hello World!

Notice how there are drastically fewer results this time, even though we’re still searching for the same string.

We could run this command multiple times and change the file extension to search for the string in multiple file types. However, we could also use curly braces ( { } ) to achieve the same thing in one line:

$ grep -ri "hello" --include=*.{cc,h} 
Test/test.h:Hello World! 
Test/test.cc:Hello World! 
test.h:Hello World! 
test.cc:Hello World!

We can include as many file types as we want by simply separating them by commas.

4. Conclusion

In this tutorial, we explored ways that we can use the grep command. We looked at searching through all files in a local directory for a specific string of text. We then learned how we could use options to search through all folders within a directory recursively. Finally, we saw how we could limit our results by using another type of option to specify the file type.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.