CLI Magic: command not found, suggest what to apt-get

May 5th, 2007 mysurface Posted in apt-get, Bash, command-not-found, which | Hits: 57171 | 7 Comments »

I have recently upgrade my Ubuntu Edgy to Ubuntu Feisty and I discover a fantastic CLI magic packaged with Feisty, its call command-not-found. With command-not-found package, when you have type some command that doesn’t exist in your terminal, it will suggest you what to install.

Let say I type trafshow, which It is not installed, the result will be this


The program 'trafshow' is currently not installed.  You can install it by typing:
sudo apt-get install netdiag
Make sure you have the 'universe' component enabled
bash: trafshow: command not found

If somehow it doesn’t work at your end, try to look at /etc/bash.bashrc, make the lines at below is uncomment.


# if the command-not-found package is installed, use it
if [ -x /usr/bin/command-not-found ]; then
        function command_not_found_handle {
                /usr/bin/command-not-found $1
                return $?
        }
fi

command-not-found itself is a python script that will return the suggestion line. Therefore if you type command-not-found <command name> , it will returns the suggestion line even the installed command.

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

With that, we can actually check on certain command is from which package, where conventionally I usually do with dpkg -S. Compare to dpkg -S, command-not-found is much much more faster.

Another thing I can make use of command-not-found in my bash script. Look the the simple script here

#!/bin/bash
Cmd=`which trafshow`;
if [ ! -n $Cmd ]
then
    command-not-found trafshow
fi

I try to check for absolute path of trafshow, if trafshow is not installed, the variable Cmd will be null, and therefore I return the suggestion line. The reason I do this is because the command not found features is not functional automatically in bash script. I mean, if you run trafshow straight away in your script and trafshow is not installed, the suggestion line will not appear.

I have no idea whether command-not-found is available on other Linux distribution besides Ubuntu Feisty, but refers to Agile Testing, you can apt-get command-not-found package in Ubuntu Edgy too.

With command-not-found feature enabled, it somehow slow down the command line process because it performs some checking every time you hit the command name that doesn’t exist. Niath is actually time the process of command-not-found, and it is not so slow that we can still tolerate the delay.

[中文翻译]

[tags]ubuntu, cli magic, command not found, suggestion apt-get, feisty, edgy[/tags]

7 Responses to “CLI Magic: command not found, suggest what to apt-get”

  1. Very cool tip. Nice..
    This was submitted in queue @ tweako ( http://www.tweako.com )

  2. [...] According to this blog post, if you use a command line at the shell that you do not have, Ubuntu Feisty will tell you what package to apt-get, that is rather handy. [...]

  3. Can it work with tcsh? How do I set it up in my .tcshrc?

  4. SEO Scrapers offers cutting-edge solutions for your search engine optimization needs and PPC / internet marketing campaigns. Services comprise of Search Engine Optimization.

  5. I have posted a complete how-to for the linux newbs at LinuxBuzz.net

  6. While command-not-found thing in bash is spectacular, it can be very frustrating, any mistake you make in command line, these thing wants to suggest something, each time checking in repository etc whether there is something to apt-get. It takes time, finally I decided to turn it off – thanks for this post, so I knew how to disable it.

  7. Good day, Cool write-up. There’s an matter with all your website in internet explorer, could possibly check this? IE however is definitely the marketplace key and a good portion of folks will omit your terrific crafting due to this problem.

Leave a Reply