Command Line Tools

grep is an acronym of Global Regular Expression Print. grep command required file or files name and pattern to search. It scans given file or files line by line and return the lines that contain the specified pattern.

This article enhances your knowledge on grep command which you have acquired from last article.

A regular expression is a way of specifying a pattern in text that can be applied to variable inputs to find all occurrences that match the pattern. A pattern is a sequence of characters.

Create a simple file and put some dummy data in it

grep sample file

Case insensitive grep search

search for sanjay

case sensative

Default grep command search is case sensitive. To make case insensitive search use -i option

case insensative

grep for words beginning and ending with

  • Use ^ to match only at the start of line.
  • Use $ to match only at the end of line.

grep begins with

grep with ^ anchor would return all the line that start from given pattern. To match exacts word use -w option

grep w options

grep ends with

grep end with

Example question:-
Find the users which use the bash shell.

grep bash

-i option makes pattern to case insensitive. That is not useful when we want to make only certain characters case insensitive from word.

In this case we can use pattern search with []. Pattern search allow us to use any combination. There are already several ready to use combination which are known as character class. Character class is not going to be test in RHCE exam so we are not including it here. Check our Linux study guide for character classes.

For example we want to search

Both s and S

Both j and J

Than we would specify them in following manner

[sS] for both s and S

[jJ] for both j and J

grep match s and j

pattern search allows us to use digit as well

grep digit

Search for two digits

grep two digit

Match at least one letter

grep one characther

Match at least one digit

grep one digit

Search for special character

Special characters need to be escaped. For example to find & we need to use it '\&'

grep special character

Example question:-
"/etc/hosts.allow" file contains the access rules for various network services. Check has access or not.

grep etc hosts


With grep command meta characters [ +, ?, |, (, ) ] loss their special meanings. For example + have a special meaning "one or more times" but if we use it with grep command it would return the line which contains +. To use meta characters with their special meanings use egrep [ acronym of Extended Global Regular Expressions Print] command.



fgrep [acronym of "Fixed-string Global Regular Expressions Print"] does NOT recognize any regular expression meta-characters as being special. For example if we want to search the line which contains (.) DOT . For grep command dot is a meta character that means 'wild-card, any single character'.



pgrep (acronym of Process-ID Global Regular Expressions Print ) scan the currently running processes and lists the process IDs which matches the provided selection criteria. For example, if I want to know the process ID of my sshd process


This article is the part of our RHCE Study guide. It is a simplified version of grep command for the RHCE exam candidate. If you are a Linux administrator or need more details about grep command please check our Linux study guide.