Beginners Guide for Grep Command in Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

The grep command is used to perform regular expressions to find out strings and patterns from the file that match a regular expression and stream the output to a new file.

Tutorial Details

DescriptionGlobalΒ RegularΒ ExpressionΒ Print
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Fedora, Manjaro, etc.
PrerequisitesGrep
Internet RequiredNo

Searching for String

Use the β€œ-i” flag with string to find the number of times it appears in the specified text file.

$ grep -i "line" file.txt

Output:

Finding lines sharing similar strings with grep using regular expressions

Counting the Number of Matches

Instead of a line, if you are more interested in knowing the number of times it is repeated, use the following command.

$ grep -c "line" file.txt 

Output:

Finding the number of times a string matched in a file

Printing Only the Matched Word in a Separate Line

The β€œ-o” or β€œβ€“only-matching” flag will only print the matched (non-empty) parts (referring to words) of a matching line in a separate line.

$ grep -o "line" file.txt 

Output:

Printing Only the Matched Word in a Separate Line

If the word appears twice in the same line, it will also be included in the separate line.

That can be very helpful to count the number of specific word occurrences in the file.

Recursively Find the String in the Directory

If you want to find out all the lines sharing a similar pattern within the directory, use the following command:

$ grep -R "line" /path/to/directory

Output:

Finding lines matching similar string from the directory

By default, the β€œ-R” flag will also search in symbolic link files, which you can avoid by replacing it with the β€œ-r” flag.

Search for an Exact String

When you use the β€œ-i” or β€œ-R” flag to search for strings inside the file, it will show them even if they are not identical.

Example: If you were searching for lines containing β€œyou” strings, you would also get the line with the β€œyourself” string.

Use the following command to get the line matching the exact β€œyou” string:

$ grep -iw "you" file.txt

Output:

Searching for an exact matching string from the file

If you were looking inside the directory, then use the β€œ-Rw” flag.

Find the Files Sharing Similar Strings

If you have multiple files and want to list them using a similar sharing string, then use the following command:

$ grep -l "line" *.txt

Output:

Finding files sharing the similar string

Find the Line Number for the Matched String

You can easily find out the line number containing the matched string, which is handy while editing the file, by using the following command.

$ grep -n "line" file.txt

Output:

Finding the line number for matched string from line

Ignoring the Matching Line for Similar Strings

To ignore the matching line sharing the similar string, one can easily do so using the following command, which will ignore all lines containing the β€œline” string.

$ grep -v "line" file.txt 

Output:

Inverting the Pattern Match

That’s all for now. If you have any questions, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.