724

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
  • 66,947
  • 30
  • 177
  • 264
David Z
  • 9,921
  • 5
  • 27
  • 22
  • 8
    I've always wondered this myself - I know with YUM you can do yum whatprovides / but I never really have found an alternative to that in Aptitude other than the Packages website – Marco Ceppi Jul 30 '10 at 13:44
  • Same on SU: http://superuser.com/questions/10997/find-what-package-a-file-belongs-to-in-ubuntu-debian – Ciro Santilli OurBigBook.com May 21 '15 at 11:07
  • None of the answers here actually cover the `Provides:` mechanism. In some more detail, a command like `mailx` or `sendmail` is not included as a binary in *any* package; instead, various packages install their own binaries and then make some of them available under a "canonical" name via `/etc/alternatives`. See also https://askubuntu.com/questions/366135/how-to-search-for-packages-that-provides-a-virtual-package – tripleee Jan 26 '18 at 04:13

10 Answers10

684

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.

Masood Khaari
  • 140
  • 1
  • 6
Ressu
  • 13,206
  • 2
  • 35
  • 27
  • 7
    I highly recommend using dlocate, which is updated daily for faster lookups. – Daniel T Chen Nov 23 '10 at 21:29
  • 29
    Hint: if you do not know the full path, but just the command name, use `which` to find he program: `dpkg -S \`which firefox\`` – Lekensteyn Jun 07 '11 at 15:38
  • This works for libraries as well, which is particularly handy if you have a binary that won't run because you can inspect it with ldd and use the full library paths to find the packages you are missing. – Alain O'Dea Oct 23 '14 at 14:41
  • 3
    @DanielTChen, but `dlocate` may not do the job, if dlocate's database is out of date. You have to call `sudo update-dlocatedb` to update it. – jarno Jun 14 '15 at 14:40
  • 3
    If `dpkg` the exact path doesn't return anything (e.g. `dpkg /usr/bin/java`), try just the executable's name (e.g. `dpkg java`). – Dan Dascalescu Jan 21 '16 at 01:05
  • Also, note that the command may be a sym-link (like to /etc/alternatives), so use the final referenced file instead. – Lars Nordin Feb 02 '18 at 21:17
  • Like [in superuser](https://superuser.com/a/467513/500826) and [in unix.SE](https://unix.stackexchange.com/a/394607/209677), this is the best alternative. [Here](https://unix.stackexchange.com/a/191983/209677) the alternative if package is not installed. If you get _dpkg-query: no path found matching pattern_ [try this](https://unix.stackexchange.com/questions/159001/how-to-find-the-package-that-installed-a-comand-if-dpkg-s-finds-no-path-beca/159002#159002) `dpkg -S "$(readlink -fn "$(which free)")"` (or a `dpkg -S command` but you'll other things). – Pablo Bianchi May 18 '18 at 22:39
  • Works but GNU/Linux man page documents `-S` at: `man dpkg-query` (not at `man dpkg`) – Elliptical view Jun 26 '18 at 02:36
  • 7
    You can also use `realpath` to resolve symlinks, like this: `dpkg -S $(realpath $(which ))`. – Lars Nyström Jan 18 '19 at 14:24
  • strangely, this doesn't work in 20.04 with ping: ```dpkg-query -S /usr/bin/ping dpkg-query: no path found matching pattern /usr/bin/ping``` – hanshenrik Feb 01 '22 at 08:36
  • @hanshenrik that's because it's a symlink. `dpkg-query -S /bin/ping` works. Hence the discussion above about using `realpath`. – David Faure Apr 26 '22 at 18:04
402

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.

Merlijn Sebrechts
  • 7,215
  • 1
  • 42
  • 67
jbowtie
  • 12,705
  • 3
  • 25
  • 31
  • 36
    In my opinion this should be the accepted answer. But in response to *Ubuntu Packages Search*, I might argue that a shell program this simple is extremely friendly and easy to remember (once you know it). If you use dpkg, apt-get, or aptitude as your standard tools, there is nothing friendly about firing up Chrome to surf the internet! – user2097818 Apr 01 '16 at 06:00
  • 5
    @user2097818 The reason this isn't the accepted answer is that my primary question is restricted to files on the system and packages which are installed. `apt-file` often finds false positives, i.e. packages that aren't installed. Of course this answer is great for the "bonus question". – David Z Apr 03 '18 at 23:36
  • 4
    For those from the Redhat side of the world - `apt-file search <>` is the closest analog to `dnf/yum whatprovides <>`. – ffledgling Apr 29 '18 at 20:14
  • I aliased `apt-file find` as `apt-find` long time ago and it's really handy! Btw, latest versions require root for `apt-file update`. – val - disappointed in SE Jun 02 '19 at 08:38
  • 8
    I guess the question is why in the world is this functionality not built into `apt`, `apt-get`, or one of the other default `apt-*` programs. It seems like key piece of any package manager's search capabilities. – theferrit32 Feb 03 '20 at 18:52
  • After running the `sudo apt install apt-file` on Ubuntu 18, I get `Package apt-file is not available, but is referred to by another package.` – ComputerScientist May 29 '20 at 21:18
  • 1
    A couple of caveats: 1) you have to install apt-file which is really annoying; 2) it is much slower than dpkg -S, eg on my system the latter takes less than half a second, whereas apt-file find takes 20 seconds! 3) most often, if you have a path on your system, then the package is installed, so why pay for the overhead of apt-file? – Oliver Oct 28 '20 at 14:11
55

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

apt-file list packagename
muru
  • 193,181
  • 53
  • 473
  • 722
ptman
  • 661
  • 4
  • 5
24

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
  • 6
moberley
  • 962
  • 9
  • 15
19

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,126
  • 1
  • 24
  • 48
A.B.
  • 89,123
  • 21
  • 245
  • 323
  • You mean, which package and not which application. The application is your package manager, e.g. Software Center. Okay. Thank you! :) – ReyKev Nov 29 '15 at 17:14
  • Thank you for all the help! None of these suggestions helped me find which package installed this/these applications though. All searches just lead back to "Tracker". I suspected it was Enthoughts' Canopy though. That mile long EULA reminded me of my old Microsoft Windows daze, with which, I am so glad I am no longer involved. I saw that Tracker was installed yesterday so, as root, I got rid of it, along with Enthoughts' Canopy and, all problems are solved. I really thank you for all your help. It is such a great thing to have such support. Thanks again! Kevin – ReyKev Nov 29 '15 at 17:22
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
  • 869
  • 10
  • 14
  • This is such a good answer that I should delete mine! – Alexx Roche Jul 30 '18 at 11:49
  • with `apt-file -x ...` you don't need to `grep` the output, hence you get slightly shorter and more convenient one liner like `apt-file -ix search "$( realpath $( which THING ) )$"` (installed packages) resp. `apt-file -ix search '/THING$'` (not installed packages). – DJCrashdummy Jun 20 '22 at 13:34
  • well, not even that my one liner are more convenient... i found one corner case in which they actually show a more useful resp. any result at all: if the binary is linked and uses a different name, the `grep` edition shows nothing while the `apt-file -x` edition still filters for the correct command/path. **||** anyway, for installed packages `dpkg -S $( which THING )` or `apt contains $( which THING )` is IMHO still the better choice. – DJCrashdummy Jun 20 '22 at 13:56
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$'
Alexx Roche
  • 281
  • 5
  • 8
  • 1
    Alexx, I love this answer. I hope you don't mind my edit. I made it also work for files that are not installed on the system. I made a backup at https://gist.github.com/RichardBronosky/41e91e858fdf070720704179aa08ed62 and will create my own answer if you revert/reject it. – Bruno Bronosky Jul 15 '18 at 05:00
  • Update: It feels slimy to copy pasta this answer and make subtle changes to it, but my edit got rejected. I hope you feel like I maintained the integrity of your answer [in mine](https://askubuntu.com/a/1057124/146273). – Bruno Bronosky Jul 18 '18 at 05:47
  • Feel free to edit or hack; anything that makes it better for you, (that's why I added MIT Licence! I don't even need credit.) – Alexx Roche Jul 30 '18 at 11:41
2

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.

Ahmad Ismail
  • 610
  • 9
  • 29
  • 2
    `apt contains` is not a valid operation, at least for `apt` v2.4.8. – Teemu Leisti Oct 23 '22 at 14:24
  • @TeemuLeisti apparently it's from linux mint. https://www.maketecheasier.com/ultimate-guide-apt-and-apt-get-commands/ – qwr Jul 10 '23 at 02:57
  • @qwr i have checked the answer after you mentioned that this is linux mint only. Yes you are right. It is not even in debian 12, apt 2.6.1. I have updated the answer. Please check. – Ahmad Ismail Jul 10 '23 at 04:10
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,806
  • 2
  • 17
  • 24
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
  • 637
  • 6
  • 7