How to use the grep command in Linux
Created
grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search for a regular expression and print matching lines), which has the same effect.
Search any line that contains the word in filename on Linux
Search
grep 'word' filename
Help
grep --help
info grep
man grep
version information
grep --version
grep -V
grep (GNU grep) 2.20 Copyright (C) 2014 Free Software Foundation, Inc.
Search and Filter Files
filtering
ifconfig | grep ens33 -A 4
Count Number of Matches
ifconfig | grep inet6 -c
the entire pattern
ifconfig | grep "RUNNING" -w
in Gzipped Files
zgrep –i error syslog.gz
grep -i 'hello world' menu.h main.c
eth0: flags=4163
The syntax for the grep command is as follows:
Usage:
grep [OPTION]... PATTERN [FILE]...
Regexp selection:
-E, --extended-regexpPATTERN is an extended regular expression (ERE)-F, --fixed-stringsPATTERN is a set of newline-separated fixed strings-G, --basic-regexpPATTERN is a basic regular expression (BRE)-P, --perl-regexpPATTERN is a Perl regular expression-e, --regexp=PATTERNuse PATTERN for matching-f, --file=FILEobtain PATTERN from FILE-i, --ignore-caseignore case distinctions-w, --word-regexpforce PATTERN to match only whole words-x, --line-regexpforce PATTERN to match only whole lines-z, --null-dataa data line ends in 0 byte, not newlineMiscellaneous:
-s, --no-messagessuppress error messages-v, --invert-matchselect non-matching lines-V, --versiondisplay version information and exit--helpdisplay this help text and exitOutput control:
-m, --max-count=NUMstop after NUM matches-b, --byte-offsetprint the byte offset with output lines-n, --line-numberprint line number with output lines--line-bufferedflush output on every line-H, --with-filenameprint the file name for each match-h, --no-filenamesuppress the file name prefix on output--label=LABELuse LABEL as the standard input file name prefix-o, --only-matchingshow only the part of a line matching PATTERN-q, --quiet, --silentsuppress all normal output--binary-files=TYPEassume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match'-a, --textequivalent to --binary-files=text-Iequivalent to --binary-files=without-match-d, --directories=ACTIONhow to handle directories; ACTION is 'read', 'recurse', or 'skip'-D, --devices=ACTIONhow to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip'-r, --recursivelike --directories=recurse-R, --dereference-recursivelikewise, but follow all symlinks--include=FILE_PATTERNsearch only files that match FILE_PATTERN--exclude=FILE_PATTERNskip files and directories matching FILE_PATTERN--exclude-from=FILEskip files matching any file pattern from FILE--exclude-dir=PATTERNdirectories that match PATTERN will be skipped.-L, --files-without-matchprint only names of FILEs containing no match-l, --files-with-matchesprint only names of FILEs containing matches-c, --countprint only a count of matching lines per FILE-T, --initial-tabmake tabs line up (if needed)-Z, --nullprint 0 byte after FILE nameContext control:
-B, --before-context=NUMprint NUM lines of leading context-A, --after-context=NUMprint NUM lines of trailing context-C, --context=NUMprint NUM lines of output context-NUMsame as --context=NUM--group-separator=SEPuse SEP as a group separator--no-group-separatoruse empty string as a group separator--color[=WHEN],--colour[=WHEN]use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto'-U, --binarydo not strip CR characters at EOL (MSDOS/Windows)-u, --unix-byte-offsetsreport offsets as if CRs were not there(MSDOS/Windows)
'egrep' means 'grep -E'. 'fgrep' means 'grep -F'. Direct invocation as either 'egrep' or 'fgrep' is deprecated.