30

I've downloaded the GNU version of find with homebrew:

brew install findutils --with-default-names

When I run which on find I get what I expect:

$ which find
/usr/local/bin/find

However, when I use find, the system falls back to OS X default /usr/bin/find, i.e.:

$ find -exec file {} \;
find: illegal option -- e
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

$ /usr/local/bin/find -exec file {} \;
.: directory

Why is this happening and how can I get find to work properly?

3
  • check your $PATH. probably the osx default one is in a dir that appears EARLIER in your $PATH than the gnu version. e.g. path=/bin;/usr/local/bin, if you have find in both, then it'll run the /bin version first Commented Oct 6, 2016 at 18:50
  • 1
    @MarcB but shouldn't which return /usr/bin/find in this case? I just checked my $PATH and ...:/usr/local/bin:/usr/bin:/bin:, so the default isn't earlier. Commented Oct 6, 2016 at 18:53
  • Brew installed GNU find is accessed by adding a "g" in front of find. gfind. which gfind /usr/local/bin/gfind which find /usr/bin/find Commented Nov 3, 2022 at 14:19

6 Answers 6

32

EDIT SEP 2023: See @Ambareesh's comment below this; my answer is now obsolete (as are many of the earlier answers).

If you still have an this problem, I would just reinstall brew from scratch using defaults (or there may be an easier way).


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,

Sign up to request clarification or add additional context in comments.

2 Comments

As of 2023, brew post-installation message says All commands have been installed with the prefix "g" so you can access it like gfind.
Now /find should be added to the PATH. Here is the actual .zshrc string for GNU findutils 4.10.0: PATH=$(brew --prefix)/opt/findutils/libexec/gnubin:$PATHPATH=$(brew --prefix)/opt/findutils/libexec/gnubin/find:$PATH
13

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

Comments

7

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

1 Comment

For completeness, the message I obtained was: Caveats: All commands have been installed with the prefix "g". If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc like: PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH"
3

I don't like changing the OS -- I don't know but some other utility might depend on the find command that comes with the OS which may cause issues down the road. So what I do is create an alias in .bashrc. So do the brew install findutils, that will put a /usr/local/bin/gfind, in your .bashrc put alias find='/usr/local/bin/gfind' Every time you type find, it will use gfind from /usr/local/bin/gfind. All you need to do to activate is to source .bashrc. or just start a new terminal. No need to reboot. I do this with a lot of the gnu utilities and makes it easy to undo, just remove the alias.

Comments

0

Bash caches the paths. Use hash -r to clear it. See https://www.gnu.org/software/bash/manual/bash.html#index-hash

Comments

-6

The solution couldn't be any easier:

A simple system restart.

5 Comments

You don't need to do a system restart. It's installed, so he just needs to refresh his PATH variable. He can do source ~/.bash_profile
Nothing was added to the PATH. See the comments to the question. Besides, your solution implies I‘m using Bash, which I‘m not.
Which shell are you using? There may be an equivalent command for your shell. Did you check the PATH from a new instance of the terminal, or one that was open before you ran the brew install command?
"Which shell" is an irrelevant distraction. With ANY shell, "reboot" is not the correct answer. This question is a highly ranked one in Google, and lots of OSX users want to give the GNU version of find precedence over the Apple-provided BSD version. If those visitors view this accepted answer, it will not help them.
🤣 Found the Windows sysadmin.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.