The grep command is one of the most useful and powerful commands in the Linux command line. It’s used to search the contents of a file (or multiple files) for a pattern or to filter the output of another command using a pattern, and finally prints all lines that contain that pattern.
The way the grep command works is that it reads the input (file or piped output of another command) line by line. It searches each line of the input for the pattern, and if it finds the pattern in that line, it’ll print that line in the output.
Search Contents of One File
grep “saveToDB” utility.js
Using RegEx in the grep Command
The pattern specified in grep command will be interpreted as RegEx, so you can use RegEx to match more complex patterns.
For instance, if you wanna match only lines that begin with “import”, and not the lines that contain “import” in the middle, you can use
grep “^import” utility.js
If you wanna match only the lines that end with “bob”, you can use
grep “bob$” utility.js
To match all lines that contain “the” and then exactly one character (like “then” and “they” or “them”), you can use
grep “the?” file.txt
Useful Flags of the grep Command
By default when you search for a pattern, all lines containing that pattern will be matched, for example when you search for “the”, “then” and “they” and “father” will be matched. If you only wanna include the exact word in the match, you can use the -w flag
grep -w “the” file.txt
If you only wanna know the count of lines that contains the pattern and don’t want those lines to be printed, you can use the -c flag.
For instance, to check how many times the word “batman” exists in the names.txt file, you can use
grep -c “batman” names.txt
Note that the -c flag returns the number of matching lines, not necessarily the number of matched patterns. In other words, if a line contains several matches, it’ll still be counted as one match.
Includes the line number of each match in the output. For instance, if you wanna know on which lines of the file the name “Brian” exists, you can use
grep -n “Brian” user_data.txt
If you wanna ignore the case sensitivity, you can use the -i flag. For instance this will match email, Email or EMAIL.
grep -i “email” user_data.txt
This flag inverts the results. In other words all the lines that don’t match the pattern will be returned.
For instance if you wanna see all the lines that don’t include “email” you can use
grep -v “email” user_data.txt
Including Context in the Output
Sometimes it’s useful to see the surrounding lines for each match to understand the context better. There are a few flags that can help:
Includes a specific number of lines that come after the match, in the output. For instance to see 4 lines after each match, you can use
grep -A 4 “email” user_data.txt
Similar to previous flag, this one is used to include some lines that come before the match, in the output.
grep -B 4 “email” user_data.txt
Finally, -C flag includes line before and after the match, in the output. For instance to see 3 lines before and after each match, you can use
grep -C 3 “email” user_data.txt
Search Mutiple Files
The grep command can search multiple files at once. To do that, you can simply list the names of the files you wanna search in. For instance to search in 3 files you can use
grep “pattern” file1 file2 file3
When you use grep on multiple files, it’ll show the file names that matched the pattern, in the output.
Instead of specifying files one by one, you can use file globbing to search multiple files. For instance to search all PHP files in the current directory, you can use
grep “pattern” *.php
Search A Directory Recursively with -r
One of the most powerful use cases of the grep command is to start from a specific directory and search the contents of all files in that directory and its sub-directories recursively for a specific pattern using the -r flag. This is extremely useful, for example when you have a large project with lots of files and directories, and you’re trying to find where a specific function is defined or used.
For instance to find where (in what file) the function “addToDB” is used, you can start the search in the project folder and use grep to do a recursive search.
grep -r “addToDB” /project_directory/
When you use the -r flag, the grep command will include the file names that include the match, before each match.