Linux - Grep Command

OptionsDescription
-cThis prints only a count of the lines that match a pattern
-hDisplay the matched lines, but do not display the filenames.
-iIgnores, case for matching
-lDisplays list of a filenames only.
-nDisplay the matched lines and their line numbers.
-vThis prints out all the lines that do not matches the pattern
-e expSpecifies expression with this option. Can use multiple times.
-f fileTakes patterns from file, one per line.
-ETreats pattern as an extended regular expression (ERE)
-wMatch whole word
-oPrint only the matched parts of a matching line, with each such part on a separate output line.
-A nPrints searched line and nlines after the result.
-B nPrints searched line and n line before the result.
-C nPrints searched line and n lines after before the result.
1grep -i "UNix" anand.txt 
2grep -c "unix" anand.txt
3grep -l "unix" *
4# Regular expression pattern
5grep "^unix" anand.txt
6grep "os$" geekfile.txt
  • grep -r pattern dir – search recursively for pattern in dir