- Quick grep reference and tutorial. Combine these simple options into longer ones as needed.
Search for match (the string 'hello') in file (called generically 'file'). Display every line that matches pattern (in this case every line containing 'hello')
grep hello file
Search for match in file and use quotes on the pattern. Not required unless you have special chars that are expanded by the shell. (in this case not required)
grep 'hello' file
grep hello file1 file2
grep hello *
Search for a match in all files in curent dir. Don't show errors if dirs are present. (grep treats dirs just as ordinary files and tries to "read" them). '-s' is for silent. Will also skip errors regarding nonexistent files.
grep -s hello *
grep hello *.py
Search for match in all files in current dir. Suppress warning if dirs are present. (it searches for 'hello' in all files. 'skip' is an action passed to '-d'). Show warnings about unexisting files.
grep -d skip hello *
grep -i Hello file
grep -v hello file
grep -iv Hello file
grep '[Yy]ear' file
Use basic regex (default). Match literal 'years+' in string. ('?+{|()' have no special meaning). Don't match 'years', 'yearss', 'yearsss', etc.
grep 'years+' file
Use extendend regex. Match 'years', 'yearss', 'yearsss', etc. ('+' means one or more of the chars before it, in this case an 's'). '?+{|()' have special meaning.
grep -E 'years+' file
egrep 'years+' file
grep -w year file
Match whole lines. Will match 'year' (where 'year' is the single word on a line. Won't match 'one year', 'goodyear'.
grep -x year file
Treat the search pattern literally, not as a regex. Will match the literal '[Yy]ear' but won't match 'year'.
grep -F '[Yy]ear' file
grep -e hello -e year file
Read search patterns from a file. Each pattern on a new line. Match all found patterns. 'patterns.txt' can have 'word' on one line, '[Yy]ear' on the second, etc.
grep -f patterns.txt file
grep -f patterns.txt -e '[Ee]xtra' file
Count matching lines for pattern (NOT matching patterns). Display a number - how many lines matched.
grep -c hello file
Count matching lines for every file except dirs (supressed with '-s'). Display how mayn lines matched (for every file). Will show multiple files with 0 or more matches.
grep -sc hello *
grep -l hello *.txt
grep -L hello *.txt
grep -m 10 hello file
Search for pattern whithin Nth lines for every file in current dir. Skip dirs. Note how we concatenate '-m' and '10'. We could've alse written them with a space, like '-m 10'
grep -sm10 hello *
Print only the matched parts, without the surrounding text. Example will print 'year', 'Year', 'YEAR', etc - each an a new line
grep -o [Yy]ear file
grep -s hello file nonexisting_file
grep -H year file
grep -h year file file2
grep -n year file
Print both line number and file name (eg: 'file:3:goodyear'). '-H' will force to display filename even if just one file (by default not shown). '-s' suppress dir missing warns.
grep -nHs year *
grep -A 2 year file
grep -B 2 year file
grep -B2 -A4 hello file
grep -C 2 year file
Force process binary files. Without this you'll get 'grep: /usr/bin/pamon: binary file matches'. (search for string 'au' in binary file)
grep -a au /usr/bin/pamon
grep --exclude=*.py --exclude=*.c year *
Include files that match this pattern. Use in conjuction with --exclude. (exclude all .py files and then include only 'main.py' in the search)
grep --exclude=*.py --include=main.py year *
Search recursively in dir (go as deep as possible, searching for files). DON'T follow symlinks. No warning about searching dirs shown.
grep -r hello
grep hello -r --exclude-dir='.git'
Seach recursively in dir. If simlynk encountered, follow it and search the file pointed by the symlink.
grep -R hello
Print total byte count before matched lines. First line (from file, not matched line) has a count of '0'. Eg: line 1 - '0:abc', line 2 '4:def'. It shows 4 because it has counted 4 bytes until now ('abc' + newline from the first file)
grep -b hello file
Search for 'hello' in files that might start with the '-' character. Without the '--' a file like '-myfile' won't be searched. WARNING - having such a file in your dir will BREAK "normal" grep functioning (eg: grep hello *
WON'T SHOW all 'hello' lines from files. Reason is that when it encounters file '-x' it treats it as an option since it expands the *
wildcard)
grep -- hello *
Sausage options 1. Search in binary files (text too but friendly toward binaries). Print byte count (or offset as grep calls it), force filename, ignorecase, show match only, also show line count, search recursively in this dir. Output is like 'hau/f:15:193:hello'
grep -abHionr hello
.