Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mrjayviper

macrumors regular
Original poster
Jul 17, 2012
245
27
So I installed GNU find via homebrew. I then created an alias called "find" and pointed it to GNU find.

Code:
    ~
    ➜  alias | grep find
    find=/usr/local/bin/gfind
    tree='find . -print | sed -e '\''s;[^/]*/;|____;g;s;____|; |;g'\'
    
    ~
    ➜  which find       
    find: aliased to /usr/local/bin/gfind
    
    ~
    ➜  find --version
    find (GNU findutils) 4.7.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Eric B. Decker, James Youngman, and Kevin Dalley.
    Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
My problem is that inside a script (tried bash and ZSH), the script always wants to use the find command supplied by Apple when I use the "find" command.

Any ideas on how to fix? Thank you.

This is my sample script
Code:
    #!/bin/bash    

    source ~/Documents/environment-setup.sh
    alias | grep find
    which find
    echo "running find --version"
    find --version
    echo "running gfind --version"
    gfind --version
This is the output of the script
Code:
    ~
    ➜  ./test.sh
    alias find='/usr/local/bin/gfind'
    /usr/bin/find
    running find --version
    find: illegal option -- -
    usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
           find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
    running gfind --version
    find (GNU findutils) 4.7.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Eric B. Decker, James Youngman, and Kevin Dalley.
    Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
 
I vaguely recall that aliases are for interactive use, not scripts. Refer to the bash man page, in its section on aliases. Here's an excerpt from one of my Mac's man pages:
Aliases are not expanded when the shell is not interactive, unless the​
expand_aliases shell option is set using shopt (see the description of​
shopt under SHELL BUILTIN COMMANDS below).​


The two alternatives that come immediately to mind are:
1. change your PATH variable.
2. define a shell function named find that runs /usr/local/bin/gfind.

bash will read from several different profile files, so you could edit one of those to change your PATH variable, placing /usr/local/bin before /usr/bin. You'd also need a hard-link or symlink in /usr/local/bin so 'find' is the same as 'gfind'.

A shell function named find can be defined in one of the bash profile files. It would basically be:
Code:
function find ()
{  /usr/local/bin/gfind "$@"; }
 
Last edited:
  • Like
Reactions: mrjayviper
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.