Most commonly used Grep Commands in LINUX terminal

In this article we will look on how to use the grep commands within the LINUX or MAC terminal. GREP is an acronym for “Global Regular Expression Print”. These commands are very helpful and comes handy when we want to perform search operations in files and directories using pattern matching.

I have also added few examples to show how these commands work so you will be able to use them effectively in your work or any projects.

The first example is the most common one where we want to search for a text inside a file we use this syntax:

grep

grep "sampleText" filename – provide the text you want to search and the file name as shown below

grep command in LINUX

The result returned is the line containing the matching text “Red”. Since this command is case sensitive the word “red” return none.

grep command in LINUX

grep -w

If I have two words containing the same text but I want to return the exact match of that word we can use -w as option.

For example here I want to search a name “Richard” but the result returned are two lines containing the word “Richard”.

Therefore if you want to return only the line containing the exact match we can use grep -w.

grep command with -w option to find unique match

grep -wi

If you want this command to perform case insensitive operation we can use option -i

grep command with -wi option for case insensitive operation

grep -win

Commonly anyone wants more information about the text they search. So option -n is very useful to check the line numbers containing the text we search. In the example below we can see that line numbers are also printed along with the text.

grep command with -win for getting the line number of the match

grep -B

When we use the above command along with option -B returns the lines before the matched text. Here -B 3 returns three lines before our match.

grep command to search lines before the match

grep -A

Now if we want to see few lines after our match then we need to use option -A followed by the number of lines.

grep command to search lines after the match

grep -C

If we want to see the context surrounding our match ie lines before and after our match then we can use option -C.

grep command to search line surrounding the match

grep command across multiple files

These are the commands for search a particular text in a single file, if we want to find a match in multiple files at once. Say we want to search a text against every file in our current directory.

For this we can use the wildcard entry * to grep the particular text in the whole directory path.

grep command across multiple files

Currently for this example I have only found the match for this text in the names.txt files. So this is a very useful command if you want to search for a text across multiple files across the directory.

From the above we could see that when we try to search the text across the sub directory it throws an error so we could just look for match only in the text file by specifying ./*.txt

grep command search using wildcard

If you have to search the match across sub-directories you can use -r recursive option.

grep command with recursive option

While working across multiple files we might only want the file that matches the information we are looking. In that case we can add on the -l option and only return the files with matches.

We can remove the -n option because it return the line number and run the same command. The result doesn’t display the match instead returns the files that contains the match

grep command to return the file of the match

Thank you for reading. Hope this article is helpful. Good luck and happy coding!