31

I have noticed that while on Ubuntu, if I type the following:

mc

and it isn't installed, I get the message below:

The program 'mc' is currently not installed. You can install it by typing: sudo apt-get install mc

However in Debian, that is not available. It just gives a "-bash: /usr/bin/mc: No such file or directory" message. How can I implement the same functionality in bash command line on Debian? Yes, I know that if it is package suggestion that I want, I can simply do a regex search using apt-cache search. However I was hoping for the simpler suggestion immediately on typing the name of the program.

As per discussions, the functionality is provided by the package command-not-found. However even after installing it, and also installing bash-completion package, this isn't available on the Debian bash shell.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Joel G Mathew
  • 912
  • 1
  • 8
  • 14

5 Answers5

34

The reason that installing command-not-found did not start providing suggestions for non-installed packages was that I had missed a small notification from dpkg as part of the install.

One is supposed to run the command update-command-not-found immediately after running apt-get install command-not-found. In fact dpkg prompts for running this command.

Joel G Mathew
  • 912
  • 1
  • 8
  • 14
  • 2
    I also had to run `apt-get update` before running `update-command-not-found` then either open a new bash session or `source /etc/bash.bash.rc`. – jordanbtucker Feb 20 '19 at 16:53
  • I did an `apt update` before `apt install command-not-found` then the `update-command-not-found`, and then started a new shell, but found I needed another `apt update` and maybe `update-command-not-found` before it started working. – Dave X Apr 11 '19 at 03:22
10

The debian package seems to be incomplete. The README says that you should source /etc/bash_command_not_found in your .bashrc. Strangely that file is not included in the package.

The debian command-not-found package is based on the ubuntu package. The ubuntu package seems to be more complete in that regard.

Content of the ubuntu version of bash_command_not_found:

command_not_found_handle() {
  if  [ -x /usr/lib/command-not-found ]; then
     /usr/lib/command-not-found -- "$1" 
     return $?
  else
     return 127
  fi        
}

Add those lines to your ~/.bashrc (or /etc/bash.bashrc) and the command-not-found feature should work.

Lesmana
  • 26,889
  • 20
  • 81
  • 86
  • 2
    The README is incorrect, Debian does not use that file. The handler is included in /etc/bash.bashrc (at least in Debian 9) which is run automatically by Bash interactive shells when they start up. – hackerb9 Sep 11 '17 at 02:08
  • Installing the `command-not-found` package (version 0.2.38-4 from debian.org) to MX Linux (Debian 9 Stretch base) did not modify /etc/bash.bashrc and subsequently didn't work until adding the lines in this answer. – Xen2050 Sep 20 '18 at 11:52
4

I solve this problem by:

First install command-not-found

sudo apt update
sudo apt install command-not-found

After that

sudo update-command-not-found
sudo apt update
1

If you're not sure if update is working correctly or if your command-not-found is installed properly, just use

sudo apt reinstall command-not-found

then

sudo apt-get update

and lastly, before updating command-not-found use terminal as a root, by

sudo -i

You'll see that your hostname (part of the phrase before @) changed to "root" Then write this command

update-command-not-found

no need to write "sudo" here, because you're already recognized as a root and sudo didn't work here for me anyway. Only root access did. Nonetheless it should work fine now.

CSDude
  • 11
  • 2
-1

In Debian, after installation of command-not-found, you should issues this command:

apt-get update

or

apt-file update

please note that the command update-command-not-found doesn't affect. Also you should logout and login again to make the new command-not-found shell scripts in /etc/profile run. Or you can run/source them manually.