Ubuntu 13.04 – Basic Command Line Techniques

This tutorial is for Ubuntu 13.04.

It is probably the same for every other version of Ubuntu and all other Linux distros, but I make no guarantee.

Linux (on which Ubuntu is based) is fundamentally a command line driven operating system. While there are graphical interfaces to many of the available commands, sometimes you need (or have no choice) to work at the command line.

There are five fundamental things everyone working at the command line should know (or at least be aware of):

  1. man
  2. pipes
  3. redirection
  4. less (or more)
  5. ls

man

man is short for manual

man is the help file.

When you want to know more about a command, you enter man <command name> to display information about the command. For example:

man ls

will bring up the man page for the command ls. You could also enter man man to display information about the man command. The man page for ls looks something like this:

To navigate through a man you use the following commands:

  • q – exit (quit) the man page
  • SPACE or f – forward to the next page
  • b – backward to the previous page
  • e – forward one line
  • y – backward one line

While there are man pages for pretty much every command, there is no man page for the command cd.

Pipes

Pipes allow you to send the displayed output from one command to another command.

In other words, when you enter a command that displays a bunch of information on the terminal (and maybe scrolls past the top of the terminal), you can use a pipe to send that information to another command.

The most common use of pipes is with the more (or, preferably, less) command. This is especially true when information scrolls past the top of the terminal.

The pipe is the vertical bar (or broken bar) symbol: ‘|‘.

For example, any of the following commands is likely display information that will scroll past the top of the terminal:

ls /bin
ls /etc
ls /lib

If we pipe the output to the more command, then we can scroll through the output one display page at a time:

ls /etc | more

Another common use of pipes is with the grep command. (grep is often used to filter out unwanted information – but I won’t be covering grep here.)

You can pipe together as many commands as you like. The output from the command on the left of the pipe is sent to the command on the right of the pipe.

Redirection

Sometimes you need to capture the information displayed by a command. This is done using redirection.

The output displayed by a command can be sent to a file instead of the display.

The redirection operator is the greater than symbol: ‘>’.

For example, suppose I want to dump a man page to file, so I can later look at (or print that file). I can do so by redirecting the man output to a file (you choose the name of the file):

man ls > ls.txt

The above command will send the output of the man page for ls to a file called ls.txt. I can then handle ls.txt the same way I handle any text file.

Another example, I could send a directory listing to a file as well:

ls > directory_list

The above command will send the output of ls to a file called directory_list. Again, the file can be handled the same way any other text file is handled.

Redirection always send stores the file in the current directory (normally, this will be your /home/<user> directory). You can write the file to another directory by either (1) changing to a different directory (using the cd command), or (2) specifying the path along with the file name. Note: you may not have permission to write in other directories, so the operation will fail.

less

less is an improved version of the more command. The main advantage of using less instead of more is that less lets you scroll backwards through data that has been piped to it; more only allows you to scroll forward through piped data. Other than that difference, less and more behave almost identically.

less is used to display information one page at a time.

Most often it is used in conjunction with pipes to prevent output from scrolling off the top of the terminal:

ls | less

less can be used to display the contents of a text file:

less <file name>

where <file name> is the name of the text file to display.

To navigate through text being displayed by less you use the following commands:

  • q – exit (quit) less
  • SPACE or f – forward to the next page
  • b – backward to the previous page (does not work if more is displaying text from a pipe and the main reason why less is preferred)
  • ls

    ls is short for list

    ls is used to display the contents of a directory.

    Directory contents are colour coded. Different files have different colours.

    The default colours (in the default Ubuntu 13.04 terminal) for common file types are:

    Note: if you or someone has configured your system, or you are using a different terminal (or a different Linux distribution), the colours and their significance may be completely different.

    There are other colour combinations in use and some colours (notably purple) are reused for other file types.