Mastering the grep command in Linux: A Comprehensive Guide with Examples


grep command in Linux means grep prints the matching lines. In the Linux operating system, searching for specific text patterns within files is a common task that is essential for system administrators, developers, and users alike.

The grep command is a powerful and versatile tool used for searching and filtering text based on patterns. It supports regular expressions, allowing users to perform complex searches with ease. In this article, we will explore the grep command in detail, learn how to use it effectively, and provide practical examples that demonstrate its capabilities in real-world scenarios.

grep command in Linux with Examples

grep command syntax

grep [OPTION…] -e PATTERNS … [FILE…]


  • Option Description
  • -b : Display the block number at the beginning of each line.
  • -c : Display the number of matched lines.
  • -h: Display the matched lines, but do not display the filenames.
  • -i : Ignore case sensitivity.
  • -l : Display the filenames, but do not display the matched lines.
  • -n : Display the matched lines and their line numbers.
  • -s : Silent mode.
  • -v : Display all lines that do NOT match.
  • -w : Match whole word.

According to the man page, the grep command in Linux prints lines that match a given pattern.

For more detailed information about the grep command, you can use:

man grep

grep command in Linux with Examples

$ grep 'devopsroles' /home/huupv/devopsroles.txt
// Search firmware in any case
root@DevopsRoles ~]# grep -i -w 'firmware' anaconda-ks.cfg 

//Search match word Linux or UNIX in any case
[root@DevopsRoles ~]# grep -i -w '^firmware' anaconda-ks.cfg

// Search Alphanumeric Characters
 [root@DevopsRoles ~]# grep "^[[:alnum:]]" anaconda-ks.cfg
 // Search Alpha Characters
 [root@DevopsRoles ~]# grep "^[[:alpha:]]" anaconda-ks.cfg
 // Search Blank Characters
 [root@DevopsRoles ~]# grep "^[[:blank:]]" anaconda-ks.cfg
 // Search Digit Characters
 [root@DevopsRoles ~]# grep "^[[:digit:]]" anaconda-ks.cfg
 // Search Lower Letters
 [root@DevopsRoles ~]# grep "^[[:lower:]]" anaconda-ks.cfg
 // Search Punctuation Characters
 [root@DevopsRoles ~]# grep "^[[:punct:]]" anaconda-ks.cfg
 // Search Uppercase Letters
 [root@DevopsRoles ~]# grep "^[[:upper:]]" anaconda-ks.cfg

How to match an only dot (.)

[root@DevopsRoles ~]# grep 'conf\.d' anaconda-ks.cfg 
 pushd /etc/dracut.conf.d

Read all files under each directory, recursively

[root@DevopsRoles ~]# grep -R "firmware" /root

To perform a case-insensitive search, use the -i option:

grep -i "search_string" filename

To search for a pattern in all files within a directory recursively, use the -r option:

grep -r "search_string" /path/to/directory

To display the line numbers of the matching lines, use the -n option:

grep -n "search_string" filename

To display lines that do not match the pattern, use the -v option:

grep -v "search_string" filename

To count the number of matching lines, use the -c option:

grep -c "search_string" filename

To use regular expressions for more complex searches, use the -E option (for extended regular expressions):

grep -E "pattern" filename


grep command in Linux is a simple command in Linux. The grep command is an indispensable tool in Linux for searching and filtering text. By mastering its options and syntax, you can efficiently locate and manipulate data within files, enhancing your productivity in system administration and development tasks.

Hopefully, this article has provided you with a clearer understanding of how to use the grep command examples effectively and apply it in your daily activities. Keep exploring and leveraging the powerful commands in Linux to improve your efficiency and proficiency in managing and processing text data. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.