

The '*' wildcard matches all files in the current directory, and the grep output from this command will show both (a) the matching filename and (b) all lines in all files that contain the string 'joe'.Īs a quick note, instead of searching all file with the "*" wildcard, you can also use grep to search all files in the current directory that end in the file extension. Our next grep command example searches for all occurrences of the text string joe within all files of the current directory: In a simple example like this, the quotes around the string fred aren't necessary, but they are needed if you're searching for a string that contains spaces, and will also be needed when you get into using regular expressions (search patterns). It will find and display all of the lines in this file that contain the text string fred, including lines that contain usernames like "fred", and also other strings like "alfred": This first grep command example searches for all occurrences of the text string 'fred' within the /etc/passwd file. The rest of this document describes many of these examples. That's the short version of the grep examples. Zgrep 'GET /blog' access_log.gz | more # same thing, case-insensitive Zgrep 'GET /blog' access_log.gz # all lines containing 'GET /blog' Zgrep foo myfile.gz # all lines containing the pattern 'foo' Grep -ril 'null' /home/al/sarah /var/www # search multiple dirsĮgrep -ril 'aja|alvin'. # similar to the previous find command does a recursive search

name "*.java,v" -exec grep -li "prevayl" \ # print all filenames of files under current dir containing 'foo', case-insensitive Multiple search strings, multiple filename patterns Locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG' # oh yeah Ls -al | grep '^d' # list all dirs in the current dirĮgrep 'apple|banana|orange' * # search for multiple patterns, all files in current dirĮgrep -i 'apple|banana|orange' * # same thing, case-insensitiveĮgrep 'score|nation|liberty|equal' gettysburg-address.txt # all lines matching multiple patterns Ps auxwww | grep -i java # all processes containing 'java', ignoring case Ps auxwww | grep httpd # all processes containing 'httpd' Grep -vi fred /etc/passwd # same thing, case-insensitive Grep -v fred /etc/passwd # find any line *not* containing 'fred' Grep -B5 -A5 "the living" gettysburg-address.txt # five lines before and ten lines after Grep -A10 "the living" gettysburg-address.txt # show all matches, and ten lines after each match Grep -B5 "the living" gettysburg-address.txt # show all matches, and five lines before each match Grep -n we gettysburg-address.txt # show line numbers as well as the matching lines Grep -il StartInterval *.plist # same thing, case-insensitive Grep -l StartInterval *.plist # show all filenames containing the string 'StartInterval' Grep '' * # find all lines in all files in the current dir with three numbers in a row Grep 'oo' * # find Foo or Goo in all files in the current dir Grep '^fred' /etc/passwd # find 'fred', but only at the start of a line Grep -i joe users.txt # find joe, Joe, JOe, JOE, etc. Grep null *.scala # search multiple files Grep fred /etc/passwd # quotes usually not when you don't use regex patterns Grep 'fred' /etc/passwd # search for lines containing 'fred' in /etc/passwd (If the Table of Contents over there on the right side is still in the way, click or tap the ‘hide’ link in its title to hide it): Abridged grep command examplesįirst up, if you don’t like reading a bunch of text and just want to see a collection of grep commands, this section is for you.
#Grep examples line number how to
I think it’s easiest to learn how to use the grep command by showing examples, so let’s dive right in. The name grep means "general regular expression parser", but you can think of the grep command as a “search” command for Unix and Linux systems: It’s used to search for text strings and regular expressions within one or more files. Linux grep FAQ: Can you share some Linux/Unix grep command examples?
