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?

up vote 10 down vote accepted

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' .

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.

  • 1
    Note that at least now grep is not included in the coreutils package in brew but in its own package called grep – fejese Nov 17 '17 at 16:54
  • Note also that the executable is called ggrep (leading g) now. – dotancohen Sep 5 at 10:08
  • 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 Sep 5 at 17:13
  • @bmike: Edited, thanks. – dotancohen Sep 5 at 17:31

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.