Notes on grep.


grep <search> <file/path>
grep "<phrase>" <file>
grep '<regexp>' <file>

most common usage

grep -inr "texttofind"

grep -ril "texttofind" *

-r : recursive
-n : show line number
-i : ignore case
-l : show only the filenames. prints only the filenames with matching text.
-v : print all lines that does NOT match the pattern


$ grep chicken *
$ grep "a phrase" file  # looks for the entire phrase inside the quote.

find markdown files with images ![]()

grep -o -c "^\!\[.*\]\(.*\)" * | awk -F: '{if ($2 > 0){print $1}}'

Search for dotfiles / hidden files

By default, grep skips .dotfiles. To include .dotfiles,

grep <search> * .*    # * searches non-dot files, and .* searches dot files

grep -r <search> .      # this will search all files, but must use "-r" and "."


grep -r<...> "texttofind" .  # ". can be replaced with * but cannot have any file pattern/glob"

To restrict grep by file extension type,

grep -r --include "*.txt" TextToFind .

# note: grep ... *.txt won't work!  use --include


Same as grep -F, searches for multiple matches. No regular expression is allowed.

fgrep "<search> \n <search> \n ... " <file>

$ fgrep "chicken"

Searches for a list of words (in file “list”) at file.txt

$ fgrep -f list file.txt


egrep is same as grep -E, Most powerful grep, uses extended reg-exp

egrep <search>... <file>
  • by default, it uses simple regex. Instead, use grep -E or egrep.
  • I use alias to make it use “egrep” all the time


$grep '^\.D[SE]$' text


'.' matches any single char.
chil. = chili, chile

'*' matches 0 or more repetition of preceding character
ap*le = ale, apple

'[ ]' matches any of the characters enclosed in brackets.
[Cc]hicken = chicken, Chicken

'^' beginning of line
^Beef = Beef at beginning of line

'$' End of line
soup$ = soup at end of line

'\ ' Treat next character as literal, not regular expression
go\.to = look for "", not "goxto"


Feature comparison of ack, ag, git-grep, GNU grep and ripgrep excellent chart on difference in usage / features


$ [data is generated] | awk '/something/'

Ag (silver searcher) is highly recommended. Ag is faster because it does better at ignoring files, while grep doesn’t ignore files well.

brew install the_silver_searcher
apt-get install silversearcher-ag

Ripgrep ripgrep combines the usability of The Silver Searcher with the raw speed of grep.

brew install ripgrep    # unoptimized mac version
choco install ripgrep  #win


  • grep for programmer, written all in Perl

Platinum Searcher

  • in Go

Misc notes

Older Gnu grep was slow (pre 2.7). Workaround

For those older grep, use:

alias grep='GREP_COLOR="1;37;45" LANG=C grep --color=auto'