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.

4

3 Answers 3

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$
2
  • 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, 2019 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. Jul 16, 2019 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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .