How to make 'grep' work like in Ubuntu?

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?


Solution 1:

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

Solution 2:

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.

  • Is it possible to get the full suite of unix options for the command line tools on OS X?
  • Removing the builtin command-line utilities from PATH, zsh
  • How to have GNU's date in OS X?
  • Make ls command sort by file type (e.g. directories and file extension)