How to tell to which package the specified file belongs to

It is good to know how to tell to which package the specified file belongs to as this knowledge could be very useful during system upgrade or unexpected system failure.

Identify specified file

Use the full path to check specified file.

$ dpkg-query -S /usr/sbin/apt-cacher-ng
apt-cacher-ng: /usr/sbin/apt-cacher-ng

The following message identify file that does not belong to any installed package.

$ dpkg -S $(which youtube-dl)
dpkg-query: no path found matching pattern /usr/local/bin/youtube-dl

Search for pattern

You can also search using just part of the filename.

$ dpkg-query -S awstats.conf
awstats: /etc/awstats/awstats.conf.local
awstats: /etc/awstats/awstats.conf
Notice that matching pattern used in the above example will be expanded to the *awstats.conf* (see notes for clues).
$ dpkg-query -S *awstats*[pn][lf]
awstats: /usr/share/doc/awstats/examples/maillogconvert.pl
awstats: /usr/share/doc/awstats/examples/awstats_updateall.pl
awstats: /usr/share/doc/awstats/examples/urlaliasbuilder.pl
awstats: /usr/share/doc/awstats/examples/geoip_generator.pl
awstats: /usr/share/awstats/tools/logresolvemerge.pl
awstats: /usr/share/doc/awstats/examples/awstats_configure.pl
awstats: /usr/share/doc/awstats/examples/apache.conf
awstats: /usr/lib/cgi-bin/awstats.pl
awstats: /usr/share/doc/awstats/examples/awstats_exportlib.pl
awstats: /etc/awstats/awstats.conf
awstats: /usr/share/awstats/tools/awstats_buildstaticpages.pl

Take a look inside specified directory

You can easily check whole directory instead of the files directly.

$ dpkg-query -S /usr/src
linux-kbuild-3.2, base-files, linux-headers-3.2.0-4-amd64, 
linux-headers-3.2.0-4-common, virtualbox-dkms: /usr/src
$ dpkg-query -S /usr/src/*
linux-headers-3.2.0-4-amd64: /usr/src/linux-headers-3.2.0-4-amd64
linux-headers-3.2.0-4-common: /usr/src/linux-headers-3.2.0-4-common
linux-kbuild-3.2: /usr/src/linux-kbuild-3.2
virtualbox-dkms: /usr/src/virtualbox-4.1.18

Notes

To understand when and how matching pattern is expanded look at the searchfiles function defined inside src/querycmd.c file in dpkg source code.

dpkg-query can be used only to query the dpkg database, although you can use full fledged dpkg command.

$ dpkg -S /usr/sbin/apt-cacher-ng
apt-cacher-ng: /usr/sbin/apt-cacher-ng
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.

Gdansk, Poland https://sleeplessbeastie.eu