Grep usage

Paul Brown · 15/05/2024 · 1 min read


Search for a given word across a given directory

grep -ir "word-to-search-for" /path/to/search/directory --exclude-dir={directory-to-ignore,head-directory-to-ignore\*}

So:

grep -ir "post" ./project-repository --exclude-dir={node_modules,vendor,tests,database\*}

The grep tool is global-regular-expressions-print, doing what it says on the tin.

  • -i flag makes any search case insensitive
  • -r tells the tool to search recursively through any subdirectories
  • The --exclude-dir option takes a string directory to not include in the search in the form of a glob (not a regular expression -- note the gotcha!) Using the braces and comma separation allowed by zsh (and probably most other modern shells, we can specify multiple directories at once.)
  • -e allows you to specify multiple patterns: grep -e "first word" -e "second word" /path/to/search/directory

Discussions

Login to Post Comments
No Comments Posted