How to find package for installed file in Brew?

How can I identify the package/formula for a given file, or a listing of all of a package's owned files, in 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.


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.


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