Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU find on MacOS [closed]

Tags:

find

macos

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?

like image 803
dangom Avatar asked Oct 06 '16 18:10

dangom


People also ask

Does macOS have GNU?

The macOS doesn't contain some GNU programs ( watch , wget , wdiff , gdb , autoconf ) and comes with BDS counterpart programs ( sed , tar , which , grep , awk , to name a few). Also, some GNU programs installed on macOS are outdated (e.g. bash , emacs , less , nano , etc.).

Can you install GNU on Mac?

Homebrew. Homebrew can be used to install the GNU versions of tools onto your Mac, but they are all prefixed with “g” by default. NOTE: 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.


4 Answers

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,

like image 117
Scott Prive Avatar answered Oct 07 '22 15:10

Scott Prive


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

like image 31
Jingguo Yao Avatar answered Oct 07 '22 14:10

Jingguo Yao


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

like image 6
Max Avatar answered Oct 07 '22 16:10

Max


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.

like image 3
Rich Avatar answered Oct 07 '22 16:10

Rich