10

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.

3 Answers3

17

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
  • 434,908
  • 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
  • 76,565
  • 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
0

apt-file, with its perl regex, is almost able to perform the task... if only the file was verified to be installed somewhere in $PATH...

apt-file find --regexp "/$COMMAND$" | grep -E "($(tr : '|' <<< "$PATH"))/$COMMAND$""
Paul
  • 210