GNU find on MacOS

brew has changed since this question was posted. The accepted answer was not correct before - not enough for someone to follow anyways (rebooting wasn't the answer) - but now the packaging of brew and pathing has also changed. To keep this page relevant, here is the new answer.

This shows MacOS provided find, which defaults in the system $PATH:

$ which find
/usr/bin/find

This installs GNU find:

$ brew install findutils

While it has been installed, it does not touch the mac version, nor will it assume the default in your path. This is to prevent surprises. You could stop here and do no additional configuration, but to target the GNU version your scripts would need to specify the full path to the executable.

Now, make GNU default (first in path), meaning just a plain find command invokes it, you will need to put the GNU find's directory "first" in your path. In my case manage $PATH in $HOME/.bash_profile but on some systems that could be $HOME/.bashrc.

PATH=$(brew --prefix)/opt/findutils/libexec/gnubin:$PATH

^^ You might have other things already "ahead" of your default PATH var. You can either add this verbatim as a new line, or carefully insert the string (including the $ and using the : to separate from the next value)

In every shell window open, reload your env:

$ source ~/.bash_profile

Or, close all your terminal windows. New terminal windows will have the updated PATH var. DO NOT reboot.

Now check your find:

$ which find
/usr/local/opt/findutils/libexec/gnubin/find

Hooray, GNU is the default find. But we did not harm the OS default find -- it is still there, so any macOS-specific scripts will still find it:

$ ls /usr/bin/find
/usr/bin/find

If you come across any non-Apple, macOS scripts that heavily assumed "find" is of the BSD type (Apple's version), but you installed GNU find first in your search path, then you will run into a compatibility problem with Brew. Because BSD 'find' has options that GNU 'find' does not, and vice-versa. Few scripts assume you use BSD find, but if you encounter this: Just add a line to the top of that script to alias find to the correct one, or you can manipulate $PATH so that find goes to the correct installed instance of the command, or if none of this sounds easy you can edit the script so that all instances of find command include the fully-qualified-path to the correct version. But need for this paragraph is rare. :-)

Be aware that --with-default-names functionality is removed (yet, confusingly, the brew website still suggests this option). With any modern Brew install, trying to use --with-default-names will just give you an error message. Things change, and unfortunately the Brew website is always seriously outdated for reasons not made clear,


Note that the --with-default-names has been removed. Now one has to explicitly add the binary to the path, as instructed when installing.


Just start a new terminal. A system restart is an overkill.

Tags:

Macos

Find