How to determine which brew package provides a given file?

Is there a way to figure out which brew package provides a given file (like apt-file on Ubuntu or pkgfile / pacman -F on Arch)?

I'm not talking about querying which locally installed package owns a file on my system, I have a particular file that I need installed (/usr/local/lib/libboost_python.dylib to be specific) and want to know which remote package would provide that file.


Solution 1:

There is not. Nothing in Homebrew maintains a list of files that a package is allowed or expected to install.

Solution 2:

As stated in this answer you can do ls -l /usr/local//lib/whatever and see where the symlink points to.

Solution 3:

While there is not apt-search like tool that allows one to find public homebrew packages that could provide a file, many brew packages are based on a linux or unix counterpart. You can search the Debian package website to find the needed package.

Example for finding package that provides file goocanvas.pc

https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=contents&keywords=goocanvas.pc

Returns libgoocanvas-dev

brew search has no results for libgoocanvas-dev or libgoocanvas but it does have a goocanvas package.

You can define a command ineed as follows that would return a list from debian.org using ineed goocanvas.pc:

ineed() {
echo -en $(echo \
    $(curl -s "https://packages.debian.org/search?suite=default&section=all&arch=any&searchon=contents&keywords=$1") \
       | sed 's%</*tr>%\\n%g') \
    | grep 'class="file"' \
    | sed 's/<[^>]*>//g' \
    | column -t \
    | grep --color -i -w "$1"
}

Solution 4:

Though there isn't a built-in brew command to search for a specific file, you can use a combination of brew list and brew list <formula> to get the answer. It takes a while, but if you're doing some cleanup and have to answer this kind of query more than once, you might do what I have done sometimes: make a catalog of files by package:

#!/bin/sh
# make a catalog of Homebrew-installed packages by package
brew list --formula |\
  while read formula; do
    brew list $formula |\
    while read file; do
      echo -e "$formula\t$file" # need -e to expand \t
    done
done

If you redirect the output of this script to a file, you can use it to search for the package that provides the file of interest.