How To Use Grep Command in Linux
This post will guide you how to use grep command to print lines matching a pattern in your Linux operating system. How do I use grep command to search a text string from a given directory under Linux or Unix systems.
- Grep Command
- Searching Text String in a File
- Searching Text String in Multiple File
- Searching Text String in Directory
- Searching Text String in All Subdirectories
- Ignoring Case in Grep Searches
- Invert Match using Grep
- Searching Exact Matching Words
- Show Number of Lines matching
- Show File Names of Matching Lines
- Searching Exact Matching Lines
Grep Command
grep searches for PATTERN in each FILE. A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input. By default, grep prints the matching lines.
The syntax of the Grep command are as followss:
grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] -e PATTERN ... [FILE...] grep [OPTIONS] -f FILE ... [FILE...]
Options:
Generic Program Information --help Output a usage message and exit. -V, --version Output the version number of grep and exit. Matcher Selection -E, --extended-regexp Interpret PATTERN as an extended regular expression (ERE, see below). -F, --fixed-strings Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched. -G, --basic-regexp Interpret PATTERN as a basic regular expression (BRE, see below). This is the default. -P, --perl-regexp Interpret the pattern as a Perl-compatible regular expression (PCRE). This is experimental and grep -P may warn of unimplemented features. Matching Control -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f (--file) option, search for all patterns given. This option can be used to protect a pattern beginning with “-”. -f FILE, --file=FILE Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (--regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing. -i, --ignore-case Ignore case distinctions, so that characters that differ only in case match each other. -v, --invert-match Invert the sense of matching, to select non-matching lines. -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non- word constituent character. Word-constituent characters are letters, digits, and the underscore. This option has no effect if -x is also specified.
Searching Text String in a File
if you want to print any matching lines from a file from the command line, and you can use grep command with a specific pattern of characters.
For example, you want to searching a patter characters “bash” from /etc/passwd file, you can run the following grep command:
$ grep "bash" /etc/passwd
Outputs:
[devops@mydevops ~]$ grep "bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
devops:x:1000:1000:devops:/home/devops:/bin/bash
You can see that grep command will print all lines where there is a match for the pattern “bash“.
Searching Text String in Multiple File
If you want to search multiple files using the grep command, and you can specified the file names that you want to search to grep command, and separated with a space character.
For example, you want to search a patter character “devops” in two files /etc/passwd, /etc/group, and you can type the following command:
$ grep "devops" /etc/passwd /etc/group
Outputs:
[devops@mydevops ~]$ grep "devops" /etc/passwd /etc/group
/etc/passwd:devops:x:1000:1000:devops:/home/devops:/bin/bash
/etc/group:wheel:x:10:devops
/etc/group:devops:x:1000:
You can see that the grep command will print the name of every file that contains the matching lines.
Searching Text String in Directory
If you want to search text string in a given directory using grep command, you can use an asterisk character instead of a filename at the end of a grep command.
For example, you want to search a pattern “devops” in /etc directory, just type the following command:
$ grep "devops" /etc/* 2>/dev/null
Outputs:
[devops@mydevops ~]$ grep "devops" /etc/* 2>/dev/null
/etc/fstab:/dev/mapper/cl_mydevops-root / xfs defaults 0 0
/etc/fstab:/dev/mapper/cl_mydevops-home /home xfs defaults 0 0
/etc/fstab:/dev/mapper/cl_mydevops-swap swap swap defaults 0 0
/etc/group:wheel:x:10:devops
/etc/group:devops:x:1000:
/etc/group-:wheel:x:10:devops
/etc/group-:devops:x:1000:
/etc/hostname:mydevops.com
/etc/mtab:/dev/mapper/cl_mydevops-root / xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
/etc/mtab:/dev/mapper/cl_mydevops-home /home xfs rw,seclabel,relatime,attr2,inode64,noquota 0 0
/etc/passwd:devops:x:1000:1000:devops:/home/devops:/bin/bash
/etc/passwd-:devops:x:1000:1000:devops:/home/devops:/bin/bash
/etc/subgid:devops:100000:65536
/etc/subuid:devops:100000:65536
Searching Text String in All Subdirectories
the above command will only search text string in all normal files under /etc direcotry, and it will not search files in subdirectories. And if you want to search all files in your given directory and its all subdirectories, and you need to pass “-r “option to the grep command, type:
$ grep -r "devops" /etc/ 2>/dev/null
Outputs:
[devops@mydevops ~]$ grep -r "devops" /etc/ 2>/dev/null
/etc/fstab:/dev/mapper/cl_mydevops-root / xfs defaults 0 0
/etc/fstab:/dev/mapper/cl_mydevops-home /home xfs defaults 0 0
/etc/fstab:/dev/mapper/cl_mydevops-swap swap swap defaults 0 0
/etc/security/limits.conf:devops soft nproc 5000
/etc/security/limits.conf:devops hard nproc 5000
/etc/group-:wheel:x:10:devops
/etc/group-:devops:x:1000:
/etc/group:wheel:x:10:devops
/etc/group:devops:x:1000:
/etc/passwd-:devops:x:1000:1000:devops:/home/devops:/bin/bash
/etc/passwd:devops:x:1000:1000:devops:/home/devops:/bin/bash
/etc/subgid:devops:100000:65536
/etc/subuid:devops:100000:65536
/etc/default/grub:GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cl_mydevops-swap rd.lvm.lv=cl_mydevops/root rd.lvm.lv=cl_mydevops/swap rhgb quiet"
/etc/hostname:mydevops.com
From the above outputs, you can see that the grep command will print the matches for all files in the current directory and its all subdirectories.
Ignoring Case in Grep Searches
the grep command is case sensitive, and if you want to ignore-case while using grep command, and you can pass the “-i” option to the grep command, type:
$ grep -i pattern FileName $ grep -i "devops" /etc/passwd
Outputs:
[devops@mydevops ~]$ grep -i devops /etc/passwd
devops:x:1000:1000:devops:/home/devops:/bin/bash
Invert Match using Grep
If you want to print the lines that do not match a given patten characters, and you can pass the “-v” option to the grep command.
For example, you want to dispaly all lines from the /etc/passwd file that do not contain the pattern characters “nologin”, and you can run the following command:
$ grep -v "nologin" /etc/passwd
Outputs:
[devops@mydevops ~]$ grep -v "nologin" /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
devops:x:1000:1000:devops:/home/devops:/bin/bash
Searching Exact Matching Words
If you want to print only lines that completely match the exact word, and you can pass the “-w” option to the grep command, type:
$ grep -w "devops" /etc/passwd
Outputs:
[devops@mydevops ~]$ grep -w "devops" /etc/passwd
devops:x:1000:1000:devops:/home/devops:/bin/bash
Searching Exact Matching Lines
If you want to force PATTERN to match only whole lines using grep command, and you need to pass the “-x” option to the grep command.
For example, you want to print only those lines that completely match the searching pattern “devops:x:1000:” in /etc/group file, and you can run the following command:
$ grep -x "devops:x:1000:" /etc/group
outputs:
[devops@mydevops ~]$ grep -x "devops:x:1000:" /etc/group devops:x:1000: [devops@mydevops ~]$ grep -x "devops" /etc/group [devops@mydevops ~]$
Show Number of Lines matching
If you want to count the number of matches using grep command, you need to pass the “-c” option to the grep command, type:
$ grep -c pattern File $ grep -c "bash" /etc/passwd
Outputs:
[devops@mydevops ~]$ grep -c "bash" /etc/passwd 2
Show File Names of Matching Lines
If you want to print the file names of the matching lines, and you can pass the “-l” option to the grep command:
$ grep -l pattern file $ grep -l "devops" /etc/* 2>/dev/null
Outputs:
[devops@mydevops ~]$ grep -l "devops" /etc/* 2>/dev/null
/etc/fstab
/etc/group
/etc/group-
/etc/hostname
/etc/mtab
/etc/passwd
/etc/passwd-
/etc/subgid
/etc/subuid
Conclusion
You should know that how to search a particular string or pattern in one or multiple files using grep command from the command line in your CentOS or RHEL or Ubuntu system.