4

What's the best way to find the uninstalled aptitude package associated with a command? I'm aware of apt-file but it's output is usually hard to parse.

For example, apt-file search ifconfig outputs 45 results. I could probably use some regex to reduce that even more, but that's far from ideal.

I'd like to be able to do something like findpkg ifconfig and see net-tools without clutter.

  • Since commands typically live in a bin (or sbin) directory, I often find it useful to simply prepend bin/ to the search term e.g. apt-file search 'bin/ifconfig' – steeldriver Oct 11 '17 at 23:03
  • @dessert provide it is kept up-to-date, apt-file will search uninstalled packages (at least for the repositories that the user has in their sources.list/sources.list.d) – steeldriver Oct 11 '17 at 23:13

1 Answers1

4

For installed packages

That's a job for dpkg-query.

dpkg-query -S /path/to/file

gives you the name of the package which installed the matched file. You'll want to use the exact path to get an output without clutter, luckily /sbin/ifconfig like in your case can be easily retrieved with which:

$ dpkg-query -S $(which ifconfig)
net-tools: /sbin/ifconfig

Reversely, dpkg-query -L PACKAGENAME prints a list of files installed by the given package, e.g.:

$ dpkg-query -L net-tools | grep ^/sbin/
/sbin/nameif
/sbin/ipmaddr
/sbin/plipconfig
/sbin/ifconfig
/sbin/route
/sbin/mii-tool
/sbin/iptunnel
/sbin/rarp
/sbin/slattach

See man dpkg-query for more.

For not yet installed packages

apt-file provides some options we can use to get rid of the clutter output:

-l, --package-only
    Only display package name; do not display file names.

-x, --regexp Treat pattern as a (perl) regular expression. See perlreref(1) for details. Without this option, pattern is treated as a literal string to search for.

Therefore, to search for a command's package the following line can be used:

apt-file -lx search "/COMMAND$"

To make it easier let's define a function:

$ findpkg() { apt-file -lx search "/$1$";}
$ findpkg ifconfig
net-tools

See man apt-file for more about this command and perldoc.perl.org for an introduction on perl regular expressions.

dessert
  • 39,982