grep in Bash

How do I use grep command in Bash?

The grep command searches the given files for lines containing a match to a given pattern list. In other words, use the grep command to search words or strings in a text files. When it finds a match in a file, it will display those line on screen.

grep examples in bash

The syntax is as follows:

grep 'word-to-search' filename
egrep 'word1|word2' /path/to/filename

Examples

Search for word vivek in /etc/passwd file, enter:
grep vivek /etc/passwd
Sample outputs:

vivek:x:500:100::/home/vivek:/bin/bash

Search for word vivek or priyanka in /etc/passwd file, enter:
egrep -w 'vivek|priyanka' /etc/passwd
Search all files in directory /nas/project for words ‘main’:
egrep -R 'main' *
OR limit search to word only
egrep -Rw 'main' *
Highlight words with color:
egrep --color -R 'main' *

Recommend readings:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz