Find text inside files using Terminal on Linux or Mac OSX

Use grep to search for some particular text inside files and folders recursively.

$ grep -rnw '/path' -e 'search term'

# Backticks to use pwd /src as the search target
$ grep -rnw `pwd`/src -e 'search term'

# As above but with exclude of a dir
$ grep -rnw --exclude-dir=node_modules -e 'search term' `pwd`

# As above but with multiple excluded dirs
$ grep -rnw --exclude-dir={node_modules,build} -e 'search term' `pwd`Code language: PHP (php)
  • -r is recursive,
  • -n is line number
  • -w is to match the whole word

Note: the path is not relative to your current directory, so you may wish to use ‘pwd’ to display your current directory path and copy paste this in as the path.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.