0

I'm trying to do a find with a -exec of a command aliased in my .bash_profile on macOS. The find command shows

find: [alias cmd]: No such file or directory

when I use

find ./ -iname *.doc -exec sha256 {} \;`

where alias sha256="shasum -a 256".

Running the unaliased command works correctly.

Is find not supposed to be able to access defined command aliases, or is this a bug in find?

2 Answers 2

4

The -exec option of find wants pure executables, while alias-es are a shell feature, meaning that they exist only when you are within a shell.

You can run any command within a shell by making -exec run bash -c command. However, for aliases to be accepted, the shell has to be run interactively.

You can trick the interactiveness by executing bash -ic sha256, but since you put your alias definition in .bash_profile you would also need to trick it as a “login shell” as well as an interactive one, because that file is only read by so-called “login shells”. That is, you should execute bash -lic sha256. But that is slower and pollutes your sessions log a bit.

Therefore I would advise you to rather put your alias definition in .bashrc file (just create it if don't have it yet), so that your alias can be used by a simpler bash -i instead of bash -li.

This would make your whole command like:

find ./ -iname *.doc -exec bash -ic 'sha256 "$1"' -- {} \;

If you really must keep your alias definition in .bash_profile then make your command like:

find ./ -iname *.doc -exec bash -lic 'sha256 "$1"' -- {} \;
0
2

No, your alias is derived from bash. find expects an executable in your PATH environment variable. (if you type which <cmd | alias> and get nothing, find will complain)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .