Learning Linux Commands

If you are a complete newbie to Linux, or if you “know enough to be dangerous,” then here are some pointers that may help.

One of the great things about Linux is that, even though the operating system is extensible through the addition of arbitrary commands, there are standards for documenting such commands. Because of these standards, not only is it easy to find out what a command does and how it works, but it’s easy to find out that these commands exist in the first place, even if you don’t know their names.

(Updated 21-May-2007: Added notes about using the type command instead of the which command.)

Commands About Commands:

  • man — Display the manual for a command
  • whatis — Display a summary of a command (rather than the entire manual)
  • apropos — Display a list of commands that pertain to (are apropos to) a keyword
  • whereis — Display information about the location of a command: the executable, the source code (if any), and the man pages.
  • which — Display which version of a command will execute (for when there are two, or more, commands with the same name installed on the system). See also the type command.
  • type — Display how a name would be interpreted if used as a command. In other words, what type of resource is it? (Actually does a more thorough job of distinguishing between multiple resources with the same name than the “which” command.)


MAN — In previous tips, we mentioned the MAN command. MAN stands for manual. It is the main resource for displaying the documentation (a.k.a. “man pages”) for any particular command. For example, to find out how the LS command works, you would enter

man ls

The man command brings up the first page of the manual, and then waits for navigation keystrokes. The most commonly used navigation controls are PageUp, PageDown, Home, End and the letter q (quit). Of course, man has its own man pages.

man man

WHATIS — The whatis command is actually an alias for using the man command with the -f switch (and -f is shorthand for –whatis). So, all of the following are equivalent.

whatis ls
man -f ls
man --whatis ls


APROPOS — The man pages for any given command usually includes a section of relevant keywords. The apropos command searches through all of the keywords listed in all of the man pages, and reports on any hits.

The apropos command is actually an alias for using the man command with the -k switch (and -k is shorthand for –apropos). So, all of the following are equivalent. (I find that it’s easier to remember “man -k” (where K stands for keyword), than it is to remember the word “apropos”.)

apropos ls
man -k ls
man --apropos ls

WHEREIS — The whereis command is immensely helpful for working with extended commands, i.e. commands that were not part of the original distribution (”distro”). The whereis command can tell you if, indeed, the command is installed on your system, and if so, where (so that you know how to invoke it). It shows you the location of any command executable, source code, and/or man pages that match the name given. Note: If you only care about searching for the command executables, then add “-b” to tell it to only search for binaries.
The whereis command

WHICH and TYPE — On the occasion that there happens to be two different versions of a command (that is, two different command executables with the same name), the which command tells you which version will actually be executed if you were to enter the command name alone (without specifying its exact location). For example, my installation of CygWin (a Linux emulator for Microsoft Windows), has two different “grep” commands installed.

Even better is the “type” command. Whereas the which command only looks for binary executables of the specified name, the type command considers that the specified name might also be a command alias or a function that would take precedence. Using type without any options returns a detailed answer (i.e. for the question, “what type of resource is this?”)

$ type grep
grep is /usr/bin/grep
$ type type
type is a shell builtin

Using type with an option of -t returns a terse answer, one of these five words: alias, keyword, function, builtin, or file.

$ type -t grep
file
$ type -t type
builtin

(The -t option is useful for checking a resource type within a shell script.)

Note: “man type” displays a combined man page for all bash builtin commands, listed alphabetically. So, use PageDown (or End and PageUp) to get to the description of the type command.

As an aside, I was about to mention what the grep command does. Instead, I think I’ll leave finding that out as an exercise to the reader.

Related articles:

Trackbacks & Pings

Comments

  1. You should never use which, it does not know about how things actually resolve (e.g., shell aliases and functions). Try “type”.

  2. Thanks for the additional tip. The which command has never steered me wrong, but now that you mention it, I see how it could. I’ll update this article shortly.

  3. the article is also missing the “info” command. Really useful if you want to get far in depth with commands.

  4. locate much better than whereis

  5. I updated this article, adding notes about the type command. (Thanks, again, Anon.)

  6. hi

    Pls send the reqd details.

    Regards,
    Shailendra.

  7. Nice list, cat is a pretty good one as well, does a lot of things other than concatenate as well.

    Cheers

  8. enjoy

  9. I have the study material of Linux OS .I have study for linux for that How to handle this Os With command.plz Send me this material/tutorials.on my mail….ur faithfully nilesh ganage.

  10. Hi, How would I find the Path MTU in a Linux machine?What is the command to change the Path MTU. Please Mail me on.

  11. can u plz send the material of linux which can be easily understandable.

  12. i studied linux material but not understand cleary pls send me material with example commands

  13. good matteral

Post a Comment


Your email is never published nor shared. Required fields are marked *



© 2006-2007 Maxim Software Corp.  All rights reserved.