9

If the package command-not-found is installed and a user tries to run a command which is not present on the system, a suggestion is printed with the name of the package which provides the executable. Is there a command with the same functionality but which takes the name of an executable as an argument?

Edit: I have read How to find out which (not installed) Debian package a file belongs to? but none of the suggestions present a command which gives an unambiguous result like command-not-found.

August Karlstrom
  • 1,736
  • 2
  • 27
  • 39
  • Possible duplicate of [How to find out which (not installed) Debian package a file belongs to?](https://unix.stackexchange.com/questions/6311/how-to-find-out-which-not-installed-debian-package-a-file-belongs-to) – muru Jul 16 '19 at 09:18
  • 1
    @muru See edit. – August Karlstrom Jul 16 '19 at 09:52
  • I don't see anything ambiguous about the output of `package: filename`, but whatever you say. – muru Jul 16 '19 at 09:57
  • @muru `apt-file search` requires and provides details which I don't care for, for instance you need to provide the path `/usr/bin` to the command in question. – August Karlstrom Jul 16 '19 at 12:18

2 Answers2

15

You can use command-not-found itself:

command-not-found --ignore-installed ls

will tell you which package contains the ls command. (--ignore-installed avoids taking into account installed packages, and in particular ensures that the command isn’t run immediately if it’s already installed.)

Alternatively, you can use apt-file:

apt-file search bin/ls

will list all packages containing a file whose path contains “bin/ls”. You can filter this to match only ls:

apt-file search bin/ls | grep bin/ls$
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • `apt-file` has options `-x` (`--regexp`) and `-F` (`--fixed-string`), so you probably don't need to pipe to `grep`. For e.g., `apt-file search -lF bin/ls` => `coreutils` – muru Jul 16 '19 at 10:03
  • @muru `-F` requires knowing the full path; `bin/` in a substring match will find binaries in `/bin`, `/sbin`, `/usr/bin` and `/usr/sbin`. `apt-file` with `-x` is very slow. – Stephen Kitt Jul 16 '19 at 11:10
4

Yes, the command is command-not-found:

$ command-not-found firefox
The program 'firefox' is currently not installed.  To run 'firefox' please ask your administrator to
install the package 'firefox-esr'
firefox: command not found

This has exactly the same functionality, because it is what the shell traps run to produce that output automatically already.

You can also use apt-file search firefox to find any matching files in a package.

Michael Homer
  • 74,824
  • 17
  • 212
  • 233
  • Which packages provides this? On Centos 7 I have `command_not_found_handle` whose definition is just a shell function – Inian Jul 16 '19 at 09:18
  • On Debian, it’s in `command-not-found`. – Stephen Kitt Jul 16 '19 at 09:18
  • One potentially annoying side-effect of this is that, if the command is available, it will be run immediately. – Stephen Kitt Jul 16 '19 at 09:19
  • @Inian Fedora has `PackageKit-command-not-found`, the name is likely the same in CentOS if it uses PackageKit. (Presumably CentOS 8 will — it's still not out yet, right? — but I'm not sure if 7 did.) ETA: And `command_not_found_handle`, which is in `/etc/profile.d/PackageKit.sh`, is definitely provided by `PackageKit-command-not-found`. – FeRD Jul 16 '19 at 20:15