How to Use the Grep Command in Linux

In the world of Linux, searching through text files to find specific content is a common task, and one that can be achieved efficiently with the grep command. Short for “Global Regular Expression Print,” grep is a powerful command-line tool that allows users to search through files using patterns defined by regular expressions.

Whether you’re looking for a particular error in a log file, or trying to locate all instances of a specific term in a large codebase, grep is the go-to tool for text searching and manipulation. With the ability to match complex patterns, filter results, and even perform operations across multiple files, grep stands as a vital utility for system administrators, programmers, and data analysts alike.

General syntax for grep command:

$ grep [OPTIONS...] [PATTERN] [FILE...]
1. Search for something within a file
grep exp FileName.txt

grep is a powerful command that allows you to search for a specific set of characters, or words exist in a file, or multiple files. The command above search for exp within FileName.txt, and return results when found.

Note: grep is by default case-sensitive, and without other parameters involved, grep would return results as long as it matches “exp”.

Example:

Assuming that FileName.txt contains the following text:

This is an example file.
The word exp is here.
No match in this line.
Expression is a good word.
Experience teaches wisdom.

The command grep exp FileName.txt would result in the following output:

This is an example file.
The word exp is here.
Expression is a good word.
Experience teaches wisdom.

This output displays all the lines in FileName.txt that contain the substring “exp”.

2. Search for something in multiple files
grep all name1.txt name2.txt name3.txt

This command expands searching to multilple specified filenames.

Example:

The command grep all name1.txt name2.txt name3.txt uses grep to search for the string “all” within the files name1.txt, name2.txt, and name3.txt. If the string is found, it will print the lines containing that string along with the file names.

name1.txt:We are all in this together.
name2.txt:All the best for your future.
name3.txt:all of these lines match.
name3.txt:All is well.
3. Finding an exact word with grep
grep -w example Example.txt

With the -w parameter, grep gets more precise in its search and only return true if the exact word matches. In the command above, grep search for "example" in Example.txt.

Any of the following would return false:

  • Example
  • examples
4. Case-insensitive search with grep
grep -i being ExampleFile.txt

With the -i parameter, grep will search in a case-insensitive manner and will return true as long the input matches, regardles if it’s lowercase or uppercase characters.

The command above searches for the word "being" in ExampleFile.txt, and will return result if found.

All the following will return true with existence of -i:

  • "Being"
  • "beING"
5. Count and output word repeatation with grep
grep -c smallness TextFile.txt

With the -c parameter, grep will first find if a specific word exist, and then count how many times it’s being repeated. The command above search for "smallness" and return the number of times it existed in TextFile.txt.

Here’s a hypothetical sample output for the given command:

5

This would mean that the word “smallness” was found in 5 lines within the TextFile.txt file. If the word “smallness” is not found in the file at all, the command would output:

0
6. Inverse search with grep
grep -v lorem sometext.txt

The parameter -v excludes the entire line that matches the input pattern, and output the rest that doesn’t contain it. The command above searches for "lorem" in sometext.txt. Any lines without "lorem" will return true.

Example:

Imagine sometext.txt contains the following lines:

lorem ipsum dolor sit amet
consectetur adipiscing elit
lorem sed do eiusmod tempor

When you run the command grep -v 'lorem' sometext.txt, the output would be:

consectetur adipiscing elit

This line is the only one that does not contain the word “lorem.”

7. Display matching line and list line number
grep -n ipsum randomtext.txt

The parameter -n returns content with line-count. When a search word is included, it returns the entire line (where word exists) with its line-count. The command above search for "ipsum" in randomtext.txt, and its output shows which line "ipsum" is at.

Example:

Assuming that randomtext.txt has the following content:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Another line without the search term.
Yet another line.
ipsum ipsum ipsum
Here's an ipsum too.

The command grep -n ipsum randomtext.txt would produce:

1:Lorem ipsum dolor sit amet, consectetur adipiscing elit.
4:ipsum ipsum ipsum
5:Here's an ipsum too.

Here, the numbers before the colons represent the line numbers in the file where the string “ipsum” was found.

8. List filenames that contain matched string
grep -l dolor *txt

With the -l parameter, only .txt extension files that contain the word "dolor" will return true. Filenames will be printed instead of the entire lioe.

Example:

Assuming you have three files in the directory, namely file1.txt, file2.txt, and file3.txt, and “dolor” is found in file1.txt and file3.txt, the output would look like this:

file1.txt
file3.txt
9. Search lines starting with a pattern
grep ^Example TextFile.txt

The character ^ in front of a search-pattern suggests grep should only look words that starts with the search-pattern and nothing else. The command above will search in TextFile.txt, and return all lines that begins with "Example".

Example:

Assuming TextFile.txt contains the following text:

Example line 1
This is another line
Example line 2
Yet another line without the keyword
Example line 3

The output of the command would be:

Example line 1
Example line 2
Example line 3
10. Multiple pattern search with grep
grep -e lorem -e amet ExampleFile.txt

The -e parameter can be used multiple times in the same command; each paired with a search-pattern, allows you to be more specific in searching for something in a file. The command above searches for the words "lorem", and "amet" in ExampleFile.txt, and return if true/found.

Example:

Assume ExampleFile.txt contains the following lines:

lorem ipsum dolor sit amet
consectetur adipiscing elit
amet, consectetur adipiscing
sed do eiusmod tempor
lorem incididunt ut

Running the command grep -e lorem -e amet ExampleFile.txt would output:

lorem ipsum dolor sit amet
amet, consectetur adipiscing
lorem incididunt ut
More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail