805

Simple enough question: is there some shell command (or GUI method) I can use that, given the path to a file on my system, tells me what package put it there? Assuming the file did in fact come from a package, that is.

Bonus question: what if it's a file that isn't installed on my system? Is there, say, a website that will let me look up a file and see what packages, if any, provide it?

Braiam
  • 69,112
David Z
  • 10,945

11 Answers11

749

You can use dpkg command to find out which installed package owns a file:

From man dpkg:

-S, --search filename-search-pattern...
                  Search for a filename from installed packages.

Example:

$ dpkg -S /bin/ls
coreutils: /bin/ls

You can either search with a full path or with just the filename.

If you wish to search for files not yet installed on your computer, you can use the Ubuntu Packages Search, or apt-file as described in a different answer.

Ressu
  • 14,086
449

The apt-file command can do this for you from the command line. I use it frequently when building packages from source. For files provided by packages that are already installed on your system, apt-cache is another choice.

To install apt-file, do:

sudo apt-get install apt-file

Then, you need to update it's database:

sudo apt-file update

And, finally, search the file:

$ apt-file find kwallet.h
kdelibs5-dev: /usr/include/kwallet.h
libkf5wallet-dev: /usr/include/KF5/KWallet/kwallet.h

However a much friendlier way is to use the Ubuntu Packages Search website. They have an option to "search the contents of packages" for a specific filename.

jbowtie
  • 14,025
58

There's also apt-file for looking up files in packages that aren't installed. For example:

apt-file list packagename
muru
  • 207,228
ptman
  • 691
27

You can search the contents of packages included in the various Ubuntu releases on the Ubuntu Packages website. Look under the heading "Search the contents of packages".

For example, here are the search results for libnss3.so in focal (20.04):

http://packages.ubuntu.com/search?searchon=contents&keywords=libnss3.so&mode=exactfilename&suite=focal&arch=any

li ki
  • 117
moberley
  • 1,002
22

You mean, which package and not which application. The application is your package manager, e.g. Software Center.

Using dpkg:

dpkg -S /usr/lib/tracker/tracker-store
dpkg -S tracker-extract
dpkg -S tracker-miner-fs

Example

% dpkg -S /usr/lib/tracker/tracker-store
tracker: /usr/lib/tracker/tracker-store

Using apt-file:

apt-file search /usr/lib/tracker/tracker-store

or also possible:

apt-file search --regex /tracker-extract$
apt-file search --regex /tracker-miner-fs$

Example

% apt-file search /usr/lib/tracker/tracker-store
tracker: /usr/lib/tracker/tracker-store

Or online here, in the section Search the contents of packages.

enter image description here

Example

enter image description here

Chai T. Rex
  • 5,323
A.B.
  • 92,125
7

This is an extension to Alexx Roche's excellent answer. I tried to make an edit to that answer, but it got rejected (though not by Alexx)


I was trying to track down what installed which on my system. After a little work I created /usr/local/bin/apt-whatprovides

#!/bin/sh
#apt-whatprovides ver. 201801010101 Copyright alexx, MIT Licence
#rdfa:deps="[realpath,apt-file,grep,which,sh,echo]"

BINARY="$(realpath $(which $@) 2>/dev/null)"
[ -z "$BINARY" ] && BINARY="$@"
echo Searching for $BINARY
PACKAGE="$(apt-file search $BINARY|grep -E ":.*[^-.a-zA-Z0-9]${BINARY}$")"
echo "${PACKAGE}"

Though for most THINGs that are installed you can just use:

apt-file search $(realpath $(which THING)) | grep 'THING$'

For THINGs that are not installed, you can use:

apt-file search THING | grep '/THING$'

The apt-whatprovides script works for files that are and are not on your system. For example, my system lacked dig but had ping so this it what resulted:

pi@raspberrypi:~ $ apt-whatprovides ping
Searching for /bin/ping
inetutils-ping: /bin/ping
iputils-ping: /bin/ping

pi@raspberrypi:~ $ apt-whatprovides dig
Searching for dig
dnsutils: /usr/bin/dig
epic4: /usr/share/epic4/script/dig
epic4-help: /usr/share/epic4/help/8_Scripts/dig
knot-dnsutils: /usr/bin/dig

Notice that Searching for is a complete path for ping (installed) and just the binary name for dig not installed. This helped me discover that I needed to install dnsutils without needing to go search https://packages.ubuntu.com/#search_contents

Bruno Bronosky
  • 939
  • 11
  • 14
4

I was trying to track down what installed which on my system. After a little work I created apt-whatprovides

#!/bin/sh
#apt-whatprovides ver. 201801010101 Copyright alexx, MIT Licence
#rdfa:deps="[realpath,apt-file,grep,which,sh,echo]"

BINARY=$(realpath $(which $@))
PACKAGE=$(apt-file search $BINARY|grep -E ":\s*${BINARY}$")
echo ${PACKAGE%:*}

Though for most THINGs you can just use

apt-file search $(realpath $(which THING))|grep 'THING$'
3

You can use:

$ apt-file --fixed-string  search /bin/ls
coreutils: /bin/ls

or,

$ apt-file --fixed-string --package-only search /bin/ls
coreutils

You can also read from file like bellow:

echo "/bin/ls" | apt-file --fixed-string --from-file --package-only search -

Instead you echo you can use like cat files.txt.

For Linux Mint you can use apt to do it.

$ apt contains /bin/ls
coreutils: /bin/ls

The output is same as:

$ dpkg -S /bin/ls
coreutils: /bin/ls

P.S. In the above commands, you can replace --fixed-string with -F; --package-only with -l and --from-file with -f.

2

I usually use dpkg -S but it only works when it's pointing at the canonical file.

DPKG can't find the file I'm looking for:

$ dpkg -S /lib/x86_64-linux-gnu/libicuuc.so.70
dpkg-query: no path found matching pattern /lib/x86_64-linux-gnu/libicuuc.so.70

Some path of the path is a symbolic link. If you don't want to inspect each parts one by one, use realpath

$ realpath /lib/x86_64-linux-gnu/libicuuc.so.70
/usr/lib/x86_64-linux-gnu/libicuuc.so.70.1

DPKG works with the real path (canonical) of the file:

$ dpkg -S /usr/lib/x86_64-linux-gnu/libicuuc.so.70.1
libicu70:amd64: /usr/lib/x86_64-linux-gnu/libicuuc.so.70.1

Or, all at once:

$ dpkg -S $(realpath /lib/x86_64-linux-gnu/libicuuc.so.70)
libicu70:amd64: /usr/lib/x86_64-linux-gnu/libicuuc.so.70.1
Gael
  • 121
2

One reason you might have to do this is if you are compiling software which there already is an ubuntu package, you can run apt-get build-dep $PACKAGENAME. That will install all packages you need to compile $PACKAGENAME.

Amandasaurus
  • 1,836
1

Why:

Different distro has its own way, too many commands to remember o(╥﹏╥)o

How:

A universal solution: pacapt -Qo file_path

Outcome:

On ubuntu:

$ pacapt -Qo /usr/bin/iostat
sysstat: /usr/bin/iostat

On centos:

$ pacapt -Qo /usr/bin/iostat
sysstat-10.1.5-19.el7.x86_64

Even can find path itself:

$ pacapt -Qo iostat
sysstat: /usr/share/man/man1/iostat.1.gz
sysstat: /usr/bin/cifsiostat
sysstat: /usr/bin/iostat
sysstat: /usr/share/man/man1/cifsiostat.1.gz

What is pacapt:

pacapt is a wrapper for many package managers

Install:

Simply download the portable script:

wget -O $HOME/bin/pacapt https://github.com/icy/pacapt/raw/ng/pacapt

Qinsi
  • 717