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

245,447

Solution 1

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

Solution 2

(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'.

Solution 3

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.

Solution 4

You can also use dlocate. From the man page;

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

Solution 5

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 ? ! )

Share:
245,447

Related videos on Youtube

Mo .
Author by

Mo .

I'm a grad student in computer science at UCSD. My research is in compilers and programming languages, and I am also intersted in kernel development and real-time 3D graphics.

Updated on September 17, 2022

Comments

  • Mo .
    Mo . over 1 year

    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?

    • Admin
      Admin almost 9 years
    • Admin
      Admin over 7 years
      dpkg -S /path/to/file if you have the package installed.
    • Admin
      Admin over 3 years
      ^ Accepted answer, better than depending on apt-file
  • John T
    John T almost 15 years
    That is only if the files already installed
  • Avio
    Avio over 10 years
    dlocate is also my favorite method, very reliable.
  • tanius
    tanius about 9 years
    In contrast to apt-file, this also finds the package to a file if it was installed manually via dpkg -i!
  • user
    user about 9 years
    This does not work for arbitrary files however, like the OP requested.
  • Jokester
    Jokester about 9 years
    FYI: apt-file seems to depend on Contents-amd64.gz provided by repositories. And not all 3rd party repos provide this file.
  • Renan Souza
    Renan Souza almost 9 years
    Now if only Raspbian also had one :)
  • jarno
    jarno over 8 years
    @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
  • DrBeco
    DrBeco about 8 years
    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.
  • Northys
    Northys over 7 years
    It did not work for me. I updated the database but it does not return anything. dpkg -S works as described below.
  • Pablo A
    Pablo A almost 7 years
    If you get dpkg-query: no path found matching pattern try this: dpkg -S "$(readlink -fn "$(which rename)")".
  • tripleee
    tripleee about 6 years
    Downvote: The link is broken and the link seems to be for RPM packages in the first place.
  • SCH
    SCH almost 6 years
    Weird, 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
  • Ctrl-C
    Ctrl-C almost 6 years
    It doesn't work with custom repos though.
  • Alexander Shcheblikin
    Alexander Shcheblikin almost 6 years
    Poor choice in regards to automation.
  • vog
    vog over 4 years
    @nnyby See comment of Pablo A
  • T-Shamspour
    T-Shamspour over 4 years
    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.
  • 71GA
    71GA over 4 years
    @Vog That solution doesn't work in my case.
  • vog
    vog over 4 years
    @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