grep command in Linux with Examples

grep command means grep prints the matching lines.


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.

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 

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

Read all files under each directory, recursively

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


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