How to get Brew formula from the filename of installed binary?

Solution 1:

The brew whence external command takes an executable and looks up the Homebrew formula from where the executable comes.

Installing

To install the brew whence command, run:

$ brew tap claui/whence

Running

To run brew whence, pass it an executable:

$ brew whence python3
Executable                Comes from    
==========                ==========    
/usr/local/bin/python3    → python 3.7.7

Or pass it several executables at once:

$ brew whence 7z git-shell lzgrep
Executable                  Comes from
==========                  ==========
/usr/local/bin/7z           → p7zip 16.02_2
/usr/local/bin/git-shell    → git 2.26.0
/usr/local/bin/lzgrep       → xz 5.2.5

More information

For more information and examples, run brew help whence:

$ brew help whence

Full disclosure

Full disclosure: I wrote this command.
It is not part of Homebrew proper but you can inspect the code yourself.