How to find package for installed file in Brew?

Homebrew

Homebrew Problem Overview


How can I identify the package/formula for a given file, or a listing of all of a package's owned files, in Homebrew?

Homebrew Solutions


Solution 1 - Homebrew

To see all files in a package:

brew ls <package>

To find the package for a file, look at the file like this:

ls -l /usr/local/bin/whatever
                   

If it was installed by Homebrew, it will be a symlink into /usr/local/Cellar/something, so that will tell you what package it's from.

Solution 2 - Homebrew

Just wrote this dirty function to get you the brew package name a file belongs to:

function brew_find_pkg {
	file_to_search="$@"
	
	for package in $(brew list); do
		brew ls $package | grep -E -q "/${file_to_search}$"
		if [ $? -eq 0 ]; then
			echo $package
			break
		fi
	done
}

Just type that in the terminal. And then to find to find the brew package a file belongs to, say the file gsed, just call the function like this

brew_find_pkg gsed

Note that the function will not work if you provide the full path of the file.

Solution 3 - Homebrew

The package name can be determined based on the symbolic link which points to the binary (Cellar/PACKAGE/...), e.g.

$ ls -la $(which awk) # => gawk
lrwxr-xr-x 1 kenorb 28 May 20  2015 /usr/local/bin/awk -> ../Cellar/gawk/4.1.1/bin/awk

$ ls -la $(which seq) # => coreutils
lrwxr-xr-x 1 kenorb 14 Apr  8  2015 /usr/local/opt/coreutils/libexec/gnubin/seq -> ../../bin/gseq

Solution 4 - Homebrew

This function works for commands stored in the $( brew --prefix )/bin directory.

function brew_find_pkg {

    cmds_to_search="$@"
    brew_bin=$( brew --prefix )/bin

    for cmd in $cmd_to_search ; do

        if [ -L $brew_bin/$cmd ] ; then
            \ls -l $brew_bin/$cmd |cut -f 2 -d '>'
        else
            echo "$cmd is not a brew command"
        fi
    done
}

E.g.:

$ brew_find_pkg gawk gcc alskdfja
 ../Cellar/gawk/4.1.4_1/bin/gawk
 ../Cellar/gcc/5.3.0/bin/gcc
alskdfja is not a brew command

Solution 5 - Homebrew

Find and brew prefix, (e.g. for opencv):

find `brew --prefix opencv3`/ -exec ls -l {} \;

(Please note judicious use of -exec, and never forget those ;'s, folks .. its how find knows that the -exec arg-list is finished..)

Solution 6 - Homebrew

Homebrew packages are installed to $(brew --prefix)/Cellar (normally /usr/local/Cellar).

To find out which packages a file belongs to, one simple approach is:

$ find $(brew --prefix)/Cellar/ | grep somefilename

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDustin OpreaView Question on Stackoverflow
Solution 1 - HomebrewPeter EisentrautView Answer on Stackoverflow
Solution 2 - HomebrewGMasterView Answer on Stackoverflow
Solution 3 - HomebrewkenorbView Answer on Stackoverflow
Solution 4 - HomebrewAphoidView Answer on Stackoverflow
Solution 5 - HomebrewibisumView Answer on Stackoverflow
Solution 6 - HomebrewEduard RozenbergView Answer on Stackoverflow