5

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.

10

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$
  • 1
    @Inian I don’t know. The question is tagged debian so I answered for Debian. – Stephen Kitt Jul 16 at 9:23
  • 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 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 at 11:10
3

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.

  • Which packages provides this? On Centos 7 I have command_not_found_handle whose definition is just a shell function – Inian Jul 16 at 9:18
  • On Debian, it’s in command-not-found. – Stephen Kitt Jul 16 at 9:18
  • One potentially annoying side-effect of this is that, if the command is available, it will be run immediately. – Stephen Kitt Jul 16 at 9: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 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.