

Let’s consider we want to count the numbers of lines which ends with false word in /etc/password file, run $ grep -c false$ /etc/passwd

If you wish to count number of lines that matches the search pattern then use ‘-c’ option in grep command. $ grep -f grep_pattern /etc/passwdġ3) Count the number of lines that matches the pattern Now try to search using grep_pattern file. Example is demonstrated below:įirst create a search pattern file with name “grep_pattern” in your current working directory. ‘-f’ option in grep command enables to take patterns from file. Mail:x:8:8:mail:/var/mail:/usr/sbin/nologin Example is listed belpw: $ grep -e nobody -e mail /etc/passwd With the help of ‘-e’ option in grep command, we can search multiple patterns in a single command. Grep command can also used to match only whole words using ‘-w’ option, example is shown below, $ sudo sysctl -a | grep -w 'vm.swappiness'Ībove command will search and look for the lines which have exactly “ vm.swappiness” word. Let’s assume we want search ‘IP_Forward’ string in nf file, run $ grep IP_Forward /etc/nf When we use ‘-i’ then it will not discriminate upper case or lower case letters while searching. ‘-i’ option in the grep command ignore case distinctions in patterns and data. To print the line numbers of empty lines, run $ grep -n '^$' /etc/nf 10) Ignore letter case while searching

Grep command can also print all the empty or blank lines from a file use the special character combination ‘^$’, example is shown below: $ grep '^$' /etc/nf etc/passwd-:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologinĪbove command will search ‘nobody’ pattern in the “/etc” directory recursively. etc/ssh/sshd_config:#AuthorizedKeysCommandUser nobody Let’s suppose, we want to search a pattern ‘nologin’ in /etc folder recursively. ‘-r’ option in grep command is used to search pattern recursively in folder and sub-folders. Linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash List all the lines of /etc/passwd that ends with “ bash” word. $ 7) Print all the lines that ends with specific wordīash shell treats dollar symbol ‘$’ as a special character which marks the ends of line or word. Let’s display the lines which starts with “backup” word in the file /etc/passwd, run $ grep ^backup /etc/passwdīackup:x:34:34:backup:/var/backups:/usr/sbin/nologin Using the option ‘-v’ in grep command, we can display the lines which don’t match the pattern $ grep -v 'nobody' /etc/passwdĦ) Print all lines that starts with specific patternīash shell treats caret symbol (^) as a special character which marks the beginning of line or a word. In below example, pattern is ‘nobody’ $ grep -n 'nobody' /etc/passwdġ8:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin Use ‘-n’ option in grep command to display line and its number which matches the pattern or word. $ 4) Display the line number with output lines $ grep -l 'root' /etc/fstab /etc/passwd /etc/mtab Let’s assume we want to list the files names which contains word ‘root’, to do so use ‘-l’ option in grep command followed by word (pattern) and files. $ sudo grep linuxtechi /etc/passwd /etc/shadow /etc/gshadowģ) Print file names that matches the pattern Run following to search ‘linuxtechi’ word in /etc/passwd, /etc/shadow and /etc/gshadow files. $ 2) Searching pattern in the multiple filesĪ word or a pattern can be searched in multiple files using grep command. Search a word “nobody” word in the file /etc/passwd file, $ grep nobody /etc/passwd When we run grep command followed by search string or pattern then it will print the matching line of a file. Without any further delay, let’s deep dive into grep command examples.
