8

Some Homebrew formulas have names that do not correspond with any of the installed commands (e.g., coreutils, speech-tools), other formulas provide an command that matches up with the name, but also provide others alongside it (e.g., lua).

Is there a simple way to determine what commands are associated with a given formula? Ideally as a brew <arg> command before installing, but even a shell script I could use post-install would help.

I thought I might be able to figure this info out with a brew link --dry-run <formula>, but that typically just gives me a warning that the formula is already linked (even with --overwrite or --force added to the command). I don't want to have to unlink each time I want to see the commands, so this route doesn't seem helpful.

1

4 Answers 4

12

As bmike's answer points out, aside from digging through the projects source to determine what executables they install, there's no good way to determine what commands come with a given formula before installing it.

After a formula is installed, running

brew unlink --dry-run formula | grep "$(brew --prefix)/bin"

is a workable option now that --dry-run is available for brew unlink.

Before that was added I wrote an external command called brew executables that still has some benefits over the above (mainly in formatting and handling some links a bit differently). I'll include a simplified (and probably non-working, due to missing some variable assignments) version of it here:

version_in_use=$(echo "$brew_info" | grep "$HOMEBREW_PREFIX.*\*$" | sed -e "s|.*$formula/\([^ ]*\).*|\1|i")

cd "$HOMEBREW_CELLAR/$formula/$version_in_use" 
for dir in `find . -type d -mindepth 1 -maxdepth 1 -name "bin"`; do
    for file in `find "$dir" -type f -o -type l -perm +111`
    do
            filename=$(basename $file)
            echo $filename
    done        
done

In short, it pulls the list of executables out of $(brew --prefix)/$formula/$version_in_use/bin. The version on my GitHub is a bit more fleshed out, including some added ability to identify/indicate when there are commands that link to each other in this bin directory.

1
  • Just a small improvement to the first command to highlight the executables themselves if you have gnu grep installed (brew install grep). Command is as follows: brew unlink --dry-run grep | ggrep --color=always -P "$(brew --prefix)/bin/\K.*" Output is as seen in this screenshot. Jul 17, 2018 at 12:10
3

I wrote Homebrew-command-not-found to do the reverse thing: get a formula from an executable. There isn’t any easy way to get a formula’s executables as other answers pointed out; so I installed all Homebrew formulae and recorded all executables in one file.

You can find it here. Each formula has its own line with the following format:

<name>: <executable-1> <executable-2> <...>

It’s as easy as grep ^git: executables.txt to get the executables installed by the git formula.

The file is regularly updated and covers 17k+ commands for 5k+ core formulae.

1

I think the accepted answer is not (or is no longer) correct, precisely.

While it's true Homebrew lacks the general feature you're asking for, there's at least one tap with at least two methods of using it:

https://github.com/Homebrew/homebrew-command-not-found

Here's what I do! Method 1:

> brew tap homebrew/command-not-found
> brew which-formula when
when
> brew install when
> when
...
Fri 2022 May 27   4:40

Method 2: eval "$(brew command-not-found-init)" # in ~/.{ba,z}shrc

N.B. There may be some significant slow-down to shells with this 2nd method.

For that reason, I enjoy using brew which-formula (method 1) when necessary.

0

In general - no.

Homebrew doesn't install package receipts which is the manner in which current tools decide what is installed where in an exhaustive and correct fashion.

Sometimes you can use brew list to check things, but I've found the best way to check on homebrew is via github - which publishes the source code for all the tools and all the packages.

My favorite search engine, Duck Duck Go even has ! commands to make it easy to search github:

!gh homebrew lua

Google does a decent job as well ranking homebrew fairly high as well as github

!g homebrew lua

Also, brew info and brew home often give you a direct link to the project source to check if your suspicions are correct. As you surmise, the real check is to uninstall / reinstall and watch. Not fun, but effective.

2
  • Could you point me out to a documentation about those ! commands? Dec 27, 2016 at 11:52
  • 1
    I am so sorry for the delay @IulianOnofrei see duckduckgo.com/bang
    – bmike
    May 28, 2022 at 0:00

You must log in to answer this question.

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