43

I tried to use grep to match one pattern on the terminal, but n o any result comes out.

$ grep -R 'networks'
grep: warning: recursive search of stdin

I knew grep on macOS is based on BSD while grep on Ubuntu is based on GNU.

How to make 'grep' work on macOS like in Ubuntu?

2 Answers 2

52

When the -R option is used, MacOS grep requires you to explicitly give it a directory to search; for example, specify . to recursively search the current directory:

grep -R 'networks' .
20

Long term, it's probably best to install the tools based on GNU or run Ubuntu virtually. Both are available for free.

brew install coreutils (to run the brew command you need to install homebrew from https://brew.sh) will put the GNU versions of the tools in your path. Look for the ggrep command and possibly a standalone grep package to install as the coreutils does tend to change over time what it includes.

(EDIT: Note that as of 2018, grep specifically needs to be installed separately from coreutils as follows:

$ brew install grep
$ ggrep sometext file.txt     # Note the executable is called ggrep with two G's

and has a unique executable name ggrep with two G's.)

Also searching here for GNU or coreutils will be helpful for context and tips/tricks.

3
  • 2
    Note that at least now grep is not included in the coreutils package in brew but in its own package called grep
    – fejese
    Commented Nov 17, 2017 at 16:54
  • 2
    Note also that the executable is called ggrep (leading g) now.
    – dotancohen
    Commented Sep 5, 2018 at 10:08
  • 1
    Thanks @dotancohen - feel free to just edit an answer if you’re sure of the situation. There is revision history and we can always chat if a change is unclear to one person or needs further review/adjustment. +1 for the comment - thanks again
    – bmike
    Commented Sep 5, 2018 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .