How to Use the Grep Command in Linux

A utility to search content input that match with one or more RegEx patterns. The content input can be provided by either passing a file path or from a standard input (stdin).

General syntax for grep command:

$ grep [OPTIONS...] [PATTERN] [FILE...]
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.

search within file

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

Example:

  • “exp” = true
  • “Exp” = false
  • “exponential” = true
  • “Expensive” = false
Search for something in multiple files
grep all name1.txt name2.txt name3.txt

This command expands searching to multilple specified filenames. The example command seach for the word "all" in name1.txt, name2.txt and name3.txt

search multiple file
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
find exact word
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"
case sensitive search
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.

count words
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.

inserve search
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.

display matching
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.

list filenames matching
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".

search pattern
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.

multiple pattern search
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail