grep — Searching for the contents of a file or files


grep [options] {search pattern} [file(s)]


  • To search for the string "Hello, World!" in file foo

    grep "Hello, World!" foo

  • To do a case insensitive search for the phrase "dumb hotel" in the file bla

    grep -i "dumb hotel" bla

  • To search recursively search all files below the current directory for Ugh:

    grep -r "Ugh" *

  • To list just the filenames that contain the word Abracadabra instead of listing the lines in the files that contain that word:

    grep -r -l "Abracadabra" *

  • To list 3 lines of context around all lines containing "FIXME" in all header and c source files in the current directory in the current directory

    grep -r -C 3 "FIXME" *.[ch]

  • To search for matches of the regular expression "^[a-z][a-z_]*[ ]*(" (i.e. try to catch function names with only lower case letters and possible underscores) in the file some-program.c

    grep "^[a-z][a-z_]*[ ]*(" some-program.c