Linux · Grep Command

Linux · Lesson

Grep Command

Master the grep command for searching text in Linux. Regular expressions and advanced usage. Practical examples for log analysis and troubleshooting.

Understanding grep command

The grep command is one of the most powerful Linux utilities used for searching and filtering text data.

It scans files or command outputs and displays lines that match a specified pattern.

Linux administrators frequently use grep for log analysis, troubleshooting, searching configuration files and processing command outputs.

Core grep concepts

Pattern searching

grep searches files and command outputs for matching patterns.

Data filtering

It helps filter useful information from large amounts of text data.

Regular expressions

grep supports regular expressions for advanced pattern matching.

Types of grep commands

grep

  • Standard Linux text searching command.
  • Supports basic regular expressions.
  • Most commonly used filtering utility.

egrep / grep -E

  • Supports extended regular expressions.
  • Useful for multiple pattern searches.
  • Recommended modern usage is grep -E.

fgrep / grep -F

  • Searches fixed strings literally.
  • Does not interpret regex symbols.
  • Recommended modern usage is grep -F.

Common grep options

Search modifiers

  • -i ignores uppercase and lowercase differences.
  • -v displays lines that do not match.
  • -w searches complete words only.
  • -n shows line numbers.

Output controls

  • -c counts matching lines.
  • -o displays only matched text.
  • ^ matches start of line.
  • $ matches end of line.

Practical Linux examples

The following examples demonstrate how to search and filter data using grep, egrep and fgrep commands.

1. Search pattern using grep

Search for matching patterns inside files using the grep command.

terminal — bash
linux-grep
[root@localhost ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

2. Count matching lines

Use the -c option to count the number of matching lines.

terminal — bash
linux-grep
[root@localhost ~]# grep -c root /etc/passwd
2
[root@localhost ~]#

3. Ignore case while searching

Use the -i option to ignore uppercase and lowercase differences.

terminal — bash
linux-grep
[root@localhost ~]# grep -i RooT /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

4. Display line numbers

Use the -n option to show line numbers with matching results.

terminal — bash
linux-grep
[root@localhost ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

5. Show only matched text

Use the -o option to display only the matched part of the line.

terminal — bash
linux-grep
[root@localhost ~]# grep -o root /etc/passwd
root
root
root
root
[root@localhost ~]#

6. Invert match

Use the -v option to display lines that do not contain the pattern.

terminal — bash
linux-grep
[root@localhost ~]# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
ok:x:1000:1000::/home/ok:/bin/bash
doc:x:1001:1001::/home/doc:/bin/bash
dom:x:1002:1002::/home/dom:/bin/bash
[root@localhost ~]#

7. Search whole words only

Use the -w option to match complete words only.

terminal — bash
linux-grep
[root@localhost ~]# grep -w ro /etc/passwd
 
[root@localhost ~]# grep ro /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

8. Match start of line

Use the ^ symbol to search for lines starting with a pattern.

terminal — bash
linux-grep
[root@localhost ~]# grep ^r /etc/passwd
root:x:0:0:root:/root:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
[root@localhost ~]#

9. Match end of line

Use the $ symbol to search for lines ending with a pattern.

terminal — bash
linux-grep
[root@localhost ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
ok:x:1000:1000::/home/ok:/bin/bash
doc:x:1001:1001::/home/doc:/bin/bash
dom:x:1002:1002::/home/dom:/bin/bash
[root@localhost ~]#

10. Search using egrep or grep -E

Use extended regular expressions to search multiple patterns.

terminal — bash
linux-grep
[root@localhost ~]# egrep -i "root|bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ok:x:1000:1000::/home/ok:/bin/bash
doc:x:1001:1001::/home/doc:/bin/bash
dom:x:1002:1002::/home/dom:/bin/bash
[root@localhost ~]#
 
[root@localhost ~]# grep -E -i "Root|Bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ok:x:1000:1000::/home/ok:/bin/bash
doc:x:1001:1001::/home/doc:/bin/bash
dom:x:1002:1002::/home/dom:/bin/bash
[root@localhost ~]#

11. Search using fgrep or grep -F

Search fixed strings without treating special characters as regular expressions.

terminal — bash
linux-grep
[root@localhost ~]# fgrep roo /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#
 
[root@localhost ~]# grep -F root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]#

Best practices while using grep

Efficient searching

  • Use specific patterns to reduce unnecessary output.
  • Combine grep with pipes for advanced filtering.
  • Use grep -i when case sensitivity is not important.

Regex handling

  • Use grep -E for extended regular expressions.
  • Use grep -F for fixed-string searches.
  • Test regular expressions carefully before using them in scripts.

Practice tasks for your Linux lab

  • Search for the word root inside /etc/passwd.
  • Count matching lines using grep -c.
  • Display matching lines with line numbers using grep -n.
  • Practice inverse matching using grep -v.
  • Search multiple patterns using grep -E.
  • Practice fixed-string searching using grep -F.

In upcoming lessons, you will continue learning Linux text processing, regular expressions and shell scripting techniques.