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.