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.
bin
(orsbin
) directory, I often find it useful to simply prependbin/
to the search term e.g.apt-file search 'bin/ifconfig'
– steeldriver Oct 11 '17 at 23:03apt-file
will search uninstalled packages (at least for the repositories that the user has in theirsources.list
/sources.list.d
) – steeldriver Oct 11 '17 at 23:13