Monday, January 18, 2010

Command not found

Most of the time, I try to live within the Linux GUI, so I can write about the experiences that any average user will have when they first try out Linux. But I've mentioned before that my background is UNIX systems administration - although now I'm in management. Still, I sometimes like to do some things at the command line, just to remind myself that I still can.

If you also like to work at the command line, you'll be interested to know about a feature in Linux that's just for you: "Command not found".

That's right, it's a feature now! But rather than just blandly tell you that your command isn't installed, Linux gives you the option to install that program.

Think of it this way: when you type a search into Google, but mis-type your query, Google recognizes that you probably meant to type something else. Google gives you a link to try your search again, with the correct text. That's pretty much what happens here - Linux recognizes that your command doesn't exist, and (after finding what you were trying to do) offers to install the right package.

For example, let's say you are at the command line, and you want to resize a digital photo to be more appropriate for displaying on a web site. So you type the "convert" command, part of ImageMagick. But your system wasn't installed with ImageMagick on it. You briefly see a "Command not found" message, then after a moment Linux asks if you want to install the ImageMagick package on your system. Answer yes, the package gets installed, and your command continues as though nothing happened.

This only works at the command line, of course. And it doesn't happen when running a script - if you mistype a command in a script, you'll get the "Command not found" message, and nothing gets installed.

This feature is provided by PackageKit-command-not-found, part of PackageKit, so standard PackageKit rules apply.

Maybe you don't want this behavior on your Linux system? Feel free to uninstall the package, and that feature just goes away. To uninstall, just go into System - Administration - Add/Remove Software, and remove the PackageKit-command-not-found package.


  1. I must say, much better than DOS/Windows' "Bad command or file name" error that is not in the least bit helpful.

    Though, it is a bit different in Ubuntu. In Ubuntu it will tell you which package your command is available in, if at all. If there isn't an exact match it'll give you a list of possible matches. It will tell you the precise apt-get command to install the package *if* you are in the "admin" group that can therefore sudo. Otherwise, it just tells you to have your sysadmin install it.

    Either way, much better than it has been in the past.

  2. That's right, it's a feature now! But rather than just blandly tell you that your command isn't installed, Linux gives you the option to install that program.

    I thought this was really cool the first time I saw it. (And just to clarify, I still do.)

    Unfortunately it's not universal, and even in distros that support it, it seems to be a new hook into Bash only, because (at least when I tried it in, I think, Kbunutu) it didn't work within Zsh. Though searching around, it looks like you can get it to, it just wasn't set up that way by default. I didn't spend a ton of time with that system. Pity either way.

    Just another illustration on why the lack of a standard package repository for Windows is, in some ways, unfortunate.

  3. Honestly, I find this feature very annoying. Compare the number of times you mistype a command versus the number of time you're trying to execute a command that isn't installed. For me, the former situation is much more numerous than the latter, but maybe I'm just a bad typist. So every time I mistype a command, I have to wait a few seconds until bash returns from searching.


Note: Only a member of this blog may post a comment.