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

Introduction

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…] PATTERNS [FILE…]
grep [OPTION…] -e PATTERNS … [FILE…]
grep [OPTION…] -f PATTERN_FILE … [FILE…]

Options

  • 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 
 -aic94xx-firmware
 -alsa-firmware
 -alsa-tools-firmware
 -ivtv-firmware
 -iwl100-firmware
 -iwl1000-firmware
 -iwl105-firmware
 -iwl135-firmware
 -iwl2000-firmware
 -iwl2030-firmware
 -iwl3160-firmware
 -iwl3945-firmware
 -iwl4965-firmware
 -iwl5000-firmware
 -iwl5150-firmware
 -iwl6000-firmware
 -iwl6000g2a-firmware
 -iwl6000g2b-firmware
 -iwl6050-firmware
 -iwl7260-firmware
 -iwl7265-firmware
 -linux-firmware

//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
 /etc/dracut.conf.d/vmware-fusion-drivers.conf
 /etc/dracut.conf.d/hyperv-drivers.conf
 /etc/dracut.conf.d/nofloppy.conf

Read all files under each directory, recursively

[root@DevopsRoles ~]# grep -R "firmware" /root
 /root/original-ks.cfg:-aic94xx-firmware
 /root/original-ks.cfg:-alsa-firmware
 /root/original-ks.cfg:-alsa-tools-firmware
 /root/original-ks.cfg:-ivtv-firmware
 /root/original-ks.cfg:-iwl100-firmware
 /root/original-ks.cfg:-iwl1000-firmware
 /root/original-ks.cfg:-iwl105-firmware
 /root/original-ks.cfg:-iwl135-firmware
 /root/original-ks.cfg:-iwl2000-firmware
 /root/original-ks.cfg:-iwl2030-firmware
 /root/original-ks.cfg:-iwl3160-firmware
 /root/original-ks.cfg:-iwl3945-firmware
 /root/original-ks.cfg:-iwl4965-firmware
 /root/original-ks.cfg:-iwl5000-firmware
 /root/original-ks.cfg:-iwl5150-firmware
 /root/original-ks.cfg:-iwl6000-firmware
 /root/original-ks.cfg:-iwl6000g2a-firmware
 /root/original-ks.cfg:-iwl6000g2b-firmware
 /root/original-ks.cfg:-iwl6050-firmware
 /root/original-ks.cfg:-iwl7260-firmware
 /root/original-ks.cfg:-iwl7265-firmware
 /root/original-ks.cfg:-linux-firmware
 /root/anaconda-ks.cfg:-aic94xx-firmware
 /root/anaconda-ks.cfg:-alsa-firmware
 /root/anaconda-ks.cfg:-alsa-tools-firmware
 /root/anaconda-ks.cfg:-ivtv-firmware
 /root/anaconda-ks.cfg:-iwl100-firmware
 /root/anaconda-ks.cfg:-iwl1000-firmware
 /root/anaconda-ks.cfg:-iwl105-firmware
 /root/anaconda-ks.cfg:-iwl135-firmware
 /root/anaconda-ks.cfg:-iwl2000-firmware
 /root/anaconda-ks.cfg:-iwl2030-firmware
 /root/anaconda-ks.cfg:-iwl3160-firmware
 /root/anaconda-ks.cfg:-iwl3945-firmware
 /root/anaconda-ks.cfg:-iwl4965-firmware
 /root/anaconda-ks.cfg:-iwl5000-firmware
 /root/anaconda-ks.cfg:-iwl5150-firmware
 /root/anaconda-ks.cfg:-iwl6000-firmware
 /root/anaconda-ks.cfg:-iwl6000g2a-firmware
 /root/anaconda-ks.cfg:-iwl6000g2b-firmware
 /root/anaconda-ks.cfg:-iwl6050-firmware
 /root/anaconda-ks.cfg:-iwl7260-firmware
 /root/anaconda-ks.cfg:-iwl7265-firmware
 /root/anaconda-ks.cfg:-linux-firmware

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

Conclusion

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 DevopsRoles.com 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.