I recently had to find out which APT package provided a specific command on one of my Ubuntu systems. After a bit of digging, I discovered that the dpkg command’s magical -S switch gives us a simple way to do this.

Per the dpkg-query man page, the -S switch (also valid as --search) does the following:

-S, --search filename-search-pattern...
    Search for packages that own files corresponding to the given pattern.  Standard shell wildchars can be used in the pattern.  This command will  not  list  extra  files  created  by  maintainer scripts, nor will it list alternatives.

In other words, it takes a file pattern (optionally including a path or portions of a path), searches the known packages to see if any have owned files that match the requested search, and lists any packages that contain a match.

For example:

[chris@home ~]$ dpkg -S /usr/bin/GET
libwww-perl: /usr/bin/GET
[chris@home ~]$ which ls
[chris@home ~]$ dpkg --search /bin/ls
coreutils: /bin/ls
[chris@home ~]$ dpkg --search /etc/cron.d
sysstat, php5-common, cron, anacron, rsnapshot: /etc/cron.d
[chris@home ~]$ dpkg --search /etc/cron.d/*
anacron: /etc/cron.d/anacron
php5-common: /etc/cron.d/php5
rsnapshot: /etc/cron.d/rsnapshot
sysstat: /etc/cron.d/sysstat
[chris@home ~]$ dpkg --search /etc/cron.daily
google-chrome-beta, man-db, update-notifier-common, apt, popularity-contest, anacron, logrotate, aptitude, cracklib-runtime, dpkg, passwd, google-musicmanager-beta, sysstat, apache2, apache2.2-common, apport, google-talkplugin, cron, samba, bsdmainutils, ntp, mlocate: /etc/cron.daily
[chris@home ~]$ which which
[chris@home ~]$ dpkg -S /usr/bin/which
debianutils: /usr/bin/which
[chris@home ~]$ which dpkg
[chris@home ~]$ dpkg -S /usr/bin/dpkg
dpkg: /usr/bin/dpkg
[chris@home ~]$ 

I added in the which command to show how you can retrieve the full path to specific commands.

Notice how supplying /etc/cron.d returned a listing of all matches for that specific string while searching for /etc/cron.d/* provides a separate match for each entry in the /etc/crond.d directory.

I really need to dig deeper into the dpkg command as it has a wealth of handy features that are just waiting to be used.

Did I help you?