4

I use Ubuntu 22.04, and I found that Ubuntu has preinstalled the command-not-found package, but the command-not-found command cannot be used.

Because Debian's command-not-found package provides the command-not-found command that looks up programs not currently installed but available from the repositories.

May I ask whether Ubuntu has modified this package?

Any answer is greatly appreciated

mook765
  • 15,925

2 Answers2

4

The command itself (actually it's an executable Python script) lives in /usr/lib/, which is not usually in a user's PATH. But you can certainly run it if you wish, e.g.

$ /usr/lib/command-not-found cpufetch
Command 'cpufetch' not found, but can be installed with:
sudo snap install cpufetch  # version 1.04, or
sudo apt  install cpufetch  # version 1.01+git20211208+40b13bc-2
See 'snap info cpufetch' for additional versions.

However it's typically run automagically by the bash shell, via its command_not_found_handle hook function:

$ declare -f -p command_not_found_handle
command_not_found_handle () 
{ 
    if [ -x /usr/lib/command-not-found ]; then
        /usr/lib/command-not-found -- "$1";
        return $?;
    else
        if [ -x /usr/share/command-not-found/command-not-found ]; then
            /usr/share/command-not-found/command-not-found -- "$1";
            return $?;
        else
            printf "%s: command not found\n" "$1" 1>&2;
            return 127;
        fi;
    fi
}
steeldriver
  • 136,215
  • 21
  • 243
  • 336
1

command-not-found is not a command to be used. If you enter a command corresponding to a package which is not installed, Ubuntu will offer you to install the package.

For example, if you write cpufetch, Ubuntu will offer you to install it with

sudo apt install cpufetch
Archisman Panigrahi
  • 28,338
  • 18
  • 105
  • 212
  • 2
    Sometimes you may need a "clean" way (ie. without typing a wrong command on a command line) to display what package provides a particular command, so there may be a few rare cases when you'd actually want to use the command-not-found command. – raj Jul 31 '23 at 17:25