This tutorial is for Ubuntu 12.04.
It should be 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):
- man
- pipes
- redirection
- more
- 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
For some reason, 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
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.<./p>
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.
more
more
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 | more
more
can be used to display the contents of a text file:
more <file name>
where <file name> is the name of the text file to display.
To navigate through text being displayed by more
you use the following commands:
- q – exit (quit)
more
- 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)
There is a slightly more useful command called less
that allows you to scroll forwards and backwards through piped information. Aside from that difference less
behaves very much like more
.
You can use man less
to learn about the less
command
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 12.04 terminal) for common file types are:
There are other colour combinations in use and some colours (notably purple) are reused for other file types.