Find what package a file belongs to in Ubuntu/Debian?

310

87

I frequently find myself missing a program, man page, or other file when working on my Ubuntu 8.04 system. Is there any simple way to look up what package contains a given file (whether it is installed already or not)? Maybe some obscure option for apt or dpkg?

Jay Conrod

Posted 2009-07-22T19:06:32.300

Reputation: 4 382

1

Same on Ubuntu: http://askubuntu.com/questions/481/how-do-i-find-the-package-that-provides-a-file

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-05-21T11:07:22.087

30dpkg -S /path/to/file if you have the package installed. – Zaz – 2016-11-09T21:35:47.613

Answers

255

apt-file search filename

or

apt-file search /path/to/file


To install apt-file, use:

sudo apt-get install apt-file

You will need to update its database before you can use it:

sudo apt-file update

John T

Posted 2009-07-22T19:06:32.300

Reputation: 149 037

8FYI: apt-file seems to depend on Contents-amd64.gz provided by repositories. And not all 3rd party repos provide this file. – Jokester – 2015-03-25T06:27:57.740

3It did not work for me. I updated the database but it does not return anything. dpkg -S works as described below. – Northys – 2016-12-28T12:36:13.840

In most cases you will not need to install a non-standard package (apt-file) and get away with using the pre-installed one (dpkg). See top voted answer. – Aleksandr Levchuk – 2019-10-17T12:50:29.443

416

(Debian/Ubuntu) Discover what package a file belongs to:

dpkg -S /usr/bin/ls

'dpkg -S' just matches the string you supply it, so just using 'ls' as an argument matches any file from any package that has 'ls' anywhere in the filename. So usually it's a good idea to use an absolute path. You can see in the second example that 12 thousand files that are known to dpkg match the bare string 'ls'.

splattne

Posted 2009-07-22T19:06:32.300

Reputation: 14 208

20In contrast to apt-file, this also finds the package to a file if it was installed manually via dpkg -i! – tanius – 2015-02-28T05:09:57.830

4

If you get dpkg-query: no path found matching pattern try this: dpkg -S "$(readlink -fn "$(which rename)")".

– Pablo A – 2017-07-07T21:02:23.997

2Weird, this didn't work for me: dpkg-query: no path found matching pattern /usr/bin/java, though there is a file at /usr/bin/java – nnyby – 2018-05-23T17:28:02.330

@nnyby See comment of Pablo A – vog – 2019-10-02T10:59:47.913

@Vog That solution doesn't work in my case. – 71GA – 2020-01-10T15:26:32.037

@nnyby Which OS and version are you using? On Debian 10 (buster) the command dpkg -S "$(readlink -fn "$(which java)")" returns openjdk-11-jre-headless:amd64: /usr/lib/jvm/java-11-openjdk-amd64/bin/java – vog – 2020-01-16T15:48:43.120

34That is only if the files already installed – John T – 2009-07-22T19:16:41.697

20

packages.debian.org is what I always use to accomplish this task. It is superior over apt-file because it can find parts of filenames as well. It's also linked up to the main packages list which will list descriptions, bugs, etc. All in all a good website. Not as useful from the command line, but still quite useful.

For speed, I bookmarked the url:

http://packages.debian.org/search?searchon=contents&keywords=%s&mode=filename&suite=unstable&arch=any

in Firefox, and added "debfind" as a keyword (click "more" in the bookmark manager with it hilighted), so I can just type "debfind " and it will work. You can change 'suite' it from unstable to stable or testing if you like, for other versions of the distribution.

jamuraa

Posted 2009-07-22T19:06:32.300

Reputation: 3 546

1It doesn't work with custom repos though. – Ctrl-C – 2018-06-29T11:40:00.047

1Poor choice in regards to automation. – Alexander Shcheblikin – 2018-08-01T16:56:04.443

11

You can also use dlocate. From the man page;

$ dlocate [ PATTERN ]
List all records where either the package name or the filename matches PATTERN.

jeremiah

Posted 2009-07-22T19:06:32.300

Reputation: 264

2@Avio dlocate does not find a recently installed package unless you run sudo update-dlocatedb first. The command is automatically run daily in /etc/cron.daily/dlocate – jarno – 2016-01-18T11:06:54.090

1dlocate is also my favorite method, very reliable. – Avio – 2013-09-16T20:14:16.200

4

I found Wajig...

wajig whichpackage /path/to/file 

eg.

wajig whichpackage /etc/apt/sources.list

Wajig is a handy command line or console tool which wraps all the apt-get, dpkg goodness that you never wanted to learn. .

To install wajig use:

apt-get install wajig

That command 'whichpackage' itself depends on apt-file

you will still need to update its database before you can use it: - I don't know yet if wajig update whixh does an apt-get update also updates the file cache, but I expect it may.

sudo wajig update

sudo apt-file update

( and Super Cow, Easter egss ? ! )

pete_m

Posted 2009-07-22T19:06:32.300

Reputation: 49

2

Simmilar to jamuraa's answer for Debian, you can also go to http://packages.ubuntu.com/ for Ubuntu. You'll have to scroll down a bit to find the "Search the contents of packages" searchbox where you can enter the path of a file.

kiri

Posted 2009-07-22T19:06:32.300

Reputation: 847

Now if only Raspbian also had one :) – XTL – 2015-05-30T10:39:50.087

0

Try searching for files using http://www.kodkast.com/applications/find-which-package-file-belongs-to

This is an application to search for files which are contained in different rpms/linux packages, and is very useful when you dont even have the rpm installed on your machine.

Ravi Agarwal

Posted 2009-07-22T19:06:32.300

Reputation: 17

Downvote: The link is broken and the link seems to be for RPM packages in the first place. – tripleee – 2018-04-17T07:01:10.397

0

The command-not-found package gives you hints about potential commands and the name of the debian package. It works by typing the command in the bash shell and looking at its output.

For example if the command name is known:

zer@ivy:~ 10:45 $ zsh5
The program 'zsh5' is currently not installed.  To run 'zsh5' please ask
your administrator to install the package 'zsh'
zsh5: command not found

and if the command is not known, some guesses are applied:

zer@ivy:~ 09:46 $ zsh
No command 'zsh' found, did you mean:
 Command 'lsh' from package 'lsh-client' (main)
 Command 'osh' from package 'omake' (main)
 Command 'ysh' from package 'libyaml-shell-perl' (main)
 Command 'ssh' from package 'openssh-client' (main)
 Command 'vsh' from package 'crystalspace' (main)
 Command 'dsh' from package 'dsh' (main)
 Command 'ash' from package 'ash' (main)
 Command 'msh' from package 'nmh' (main)
 Command 'zssh' from package 'zssh' (main)
 Command 'qsh' from package 'gridengine-client' (main)
 Command 'sh' from package 'dash' (main)
 Command 'bsh' from package 'bsh' (main)
zsh: command not found

erwing

Posted 2009-07-22T19:06:32.300

Reputation: 21

This does not work for arbitrary files however, like the OP requested. – a CVn – 2015-03-12T12:26:14.687

This is a problematic tip if the program does harmful things and IS installed. Adding -h or --help at the end would help a bit. If adding -h at the end would make this safe enough I would recommend, but even so it is not a good way to achieve what the OP asks. – Dr Beco – 2016-04-27T15:51:31.090