40

I know there's a which command, that echoes the full name of a binary (e.g. which sh). However, I'm fairly sure there's a command that echoes the package that provides a particular binary. Is there such a command? If so, what is it? I'd like to be able to run this:

commandName ls

and get

coreutils

for example.

bertieb
  • 7,344
  • 36
  • 42
  • 54
Delan Azabani
  • 1,066
  • 1
  • 10
  • 17

2 Answers2

46

I guess you are looking for the dpkg -S command (also see frequently used options for dpkg).

nik
  • 55,788
  • 10
  • 98
  • 140
11

If you want to find files in a package that you haven't installed, use apt-file

apt-get install -y apt-file
apt-file update

Then, to find something:

apt-file search /usr/bin/file

or

apt-find search file

Where "file" is the name of whatever you're searching for.

If you don't feel like going through this on every debian system, you can use this script:

#!/bin/bash
which apt-get >/dev/null || { echo apt-get not found >&2; exit 1; }
which apt-file >/dev/null || { apt-get install -y apt-file;  apt-file update; }
unset i; IFS=$'\x0a'; select i in $( apt-file search "/$@" ); do 
    test -n "$i" || break; apt-get install "${i%% *}"; done

I just whipped that up then, but it seems to work well.

Note: "dpkg -S" only finds things that you've already installed.

Orwellophile
  • 499
  • 5
  • 9
  • 3
    Using the `-y` parameter is dangerous, it could say yes to any number of things. In the script it might be fine, but the first command should really read `apt-get install apt-file` without the `-y`as you can't know what the user's system looks like. – jmiserez Jun 11 '15 at 14:57
  • @jmiserez that's a value judgement, and purely subjective opinion that should not be applied as a fits-all rule. `apt-get install -y linux-*` would be extremely bad, `apt-get install -y fortune` is quite different. Please also not that these are being used in a `bash/sh script` context beneath, where superfluous user confirmation is end-of-game. – Orwellophile Apr 16 '21 at 05:31
  • For arch based systems, check out this [link](https://unix.stackexchange.com/questions/14858/in-arch-linux-how-can-i-find-out-which-package-to-install-that-will-contain-file). `pkgfile` or `pacman -F` options are the ones I tested. I have added this comment because a google search brought me here instead of in the other one. – Ulterno Mar 05 '22 at 09:48