Skip the navigation

Linux Command-Line Cheat Sheet

By Benjamin Mako Hill and Jono Bacon
August 14, 2007 12:00 PM ET
Getting Help on the Command Line

This section provides you with some tips for getting help on the command line. The commands --help and man are the two most important tools at the command line.
Virtually all commands understand the -h (or --help) option, which produces a short usage description of the command and its options, then exits back to the command prompt. Try man -h or man --help to see this in action.
Every command and nearly every application in Linux has a man (manual) file, so finding such a file is as simple as typing man command to bring up a longer manual entry for the specified command. For example, man mv brings up the mv (move) manual.
Some helpful tips for using the man command include the following.
  • Arrow keys: Move up and down the man file by using the arrow keys.

  • q: Quit back to the command prompt by typing q.

  • man man: man man brings up the manual entry for the man command, which is a good place to start!

  • man intro: man intro is especially useful. It displays the Introduction to User Commands, which is a well-written, fairly brief introduction to the Linux command line.


There are also info pages, which are generally more in-depth than man pages. Try info info for the introduction to info pages.

Searching for Man Files

If you aren't sure which command or application you need to use, you can try searching the man files.
  • man -k foo: This searches the man files for "foo". Try man -k nautilus to see how this works.
    Note: man -k foo is the same as the apropos command.

  • man -f foo: This searches only the titles of your system's man files. Try man -f gnome, for example.
    Note: man -f foo is the same as the whatis command.


Using Wildcards

Sometimes you need to look at or use multiple files at the same time. For instance, you might want to delete all .rar files or move all .odt files to another directory. Thankfully, you can use a series of wildcards to accomplish such tasks.
  • * matches any number of characters. For example, *.rar matches any file with the ending .rar.

  • ? matches any single character. For example, ?.rar matches a.rar but not ab.rar.

  • [characters] matches any of the characters within the brackets. For example, [ab].rar matches a.rar and b.rar but not c.rar.

  • [!characters] matches any characters that are not listed. For example, [!ab].rar matches c.rar but not a.rar or b.rar.


Executing Multiple Commands

Often you may want to execute several commands together, either by running one after another or by passing output from one to another.
Running Sequentially
If you need to execute multiple commands in sequence but don't need to pass output between them, there are two options based on whether or not you want the subsequent commands to run only if the previous commands succeed or not. If you want the commands to run one after the other regardless of whether or not preceding commands succeed, place a ; between the commands. For example, if you want to get information about your hardware, you could run lspci ; lsusb, which would output information on your PCI buses and USB devices in sequence.
However, if you need to conditionally run the commands based on whether the previous command has succeeded, insert && between commands. An example of this is building a program from source, which is traditionally done with ./configure, make, and make install. The commands make and make install require that the previous commands have completed successfully, so you would use ./configure && make && make install.



The Linux Command Line



Passing Output
If you need to pass the output of one command so that it goes to the input of the next, after the character used between the commands, you need something called a pipe, which looks like a vertical bar or pipe (|).
To use the pipe, insert the | between each command. For example, using the | in the command ls | less allows you to view the contents of the ls more easily.

Moving to More Advanced Uses of the Command Line

There are a great number of good books out there for working the command line. In addition, because most of the command line has not changed in many years, a large body of information is available on the Internet. If you need help with something, often simply searching for the command will turn up what you need.
To get you started, here are some recommendations.


This content was excerpted from the new second edition of The Official Ubuntu Book, authored by Benjamin Mako Hill and Jono Bacon, with Ivan Krstic, David Murphy, Jonathan Jesse, Peter Savage and Corey Burger, published by Prentice Hall Professional, July 2007. Copyright 2007 Canonical Ltd., all rights reserved.
Our Commenting Policies