Skip to content

Instantly share code, notes, and snippets.

@bradgreens
Last active November 10, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradgreens/726cdbc03436ac8fd5ffd6b0160c4ec3 to your computer and use it in GitHub Desktop.
Save bradgreens/726cdbc03436ac8fd5ffd6b0160c4ec3 to your computer and use it in GitHub Desktop.

grep examples that don't suck, er, they're so simple they kinda suck

Minimal use cases

In these examples, the is the string we're searching for.

Search one file

grep the README.md

Search files in current directory, non-recursive

grep the *

Search files in current directory, recursive

grep -r the *

Search case insensitive

grep -i the *

Search and only display the file name results

grep -l the *

Search files in current directory, recursive, exclude certain folders

grep -r --exclude-dir={.git,node_modules,dist} the *

Display additional lines in output

grep -A 7 foo # shows 7 lines after match

grep -B 7 foo # shows 7 lines before match

grep -C 7 foo # shows 7 lines before & after match

Complex use cases

Case-insensitive recursive search in current directory, ommit unwanted folders, report the filenames only

grep -rli --exclude-dir={node_modules,spec,.node-gyp,dist,public,tmp,vendor,.next} foo ./

Resources

Random Pastes

find . -name *.ts | xargs grep datSearchTerm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment