‘-r’ option in grep command is used to search pattern recursively in folder and sub-folders. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |