grep command in Linux with Examples

grep command means grep prints the matching lines.

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.

In man page the describe it

  • grep – print lines that match patterns.
  • man grep – More details information about grep command.

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 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

Conclusion

grep command is the simple the command in Linux. It is the most popular in use terminal Linux print lines that match patterns.

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 *

Comment moderation is enabled. Your comment may take some time to appear.

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