Using the Command Line

This section is a brief tutorial on using the Linux command line. I've included it since most of the time when someone is helping you work through a problem, they will be giving you instructions to type in at the command line. Though it can be intimidating at first, using the command line is actually quite easy.

Getting A Terminal Window

Before you can use the command line, you have to have a terminal session that gives you a command prompt. That is done easily on most Linux systems. All you have to do is right-click on any blank area of the desktop. A menu will appear. One of the choices will be "Open Terminal" or something similar. Click on that selection. Voila, a terminal window will appear with a command line prompt.

Figure 12. Terminal Window

Terminal Window.

Command Line Basics

One of the great things about the command line interface in Linux is that the interface has been used by the same programmers that were maintaining it. Those programmers worked very hard to make using the command line as simple as possible and to give the command line interpreter, called the shell, lots of handy abilities. The most commonly used shell today is the Bash shell, so that's the one we'll talk about. If I tried to explain all the capabilities of the Bash shell, I'd wind up writing a book. In fact if you open the file /usr/share/doc/bash-2.05b/bashref.html (It might be named slightly differently on your system.) you'll find the Bash reference manual which is a couple hundred pages of documentation.

Fortunately, you don't need to read the entire Bash manual. Just a few tips will go a long way towards making your life easier when you are using the command line. The first handy thing to know is that Bash keeps a history of the commands you use. You can see the history of commands at any time by entering the command history. You'll get something like the following, except it will likely be a lot more lines.

Figure 13. Example History Output

 1030  cd usr/share/doc
 1031  ls
 1032  cd /usr/share
 1033  ls
 1034  cd doc
 1035  ls
 1036  cd bash-2.05b/
 1037  ls
 1038  mozilla bash.html
 1039  history
[rben@Prot bash-2.05b]$

The history, as you can see if you try this out on your own system, is listed in order. The most recent commands are the last ones shown. You can use the up and down keys to move through your command history. When you press the up key, the most recent command will replace whatever is on the command line, pressing down will restore the command line. You can move back through the command history quite a ways. The exact number of commands recorded in the history is controlled by a shell variable.

The command history also keep commands from previous sessions, so you can log out, log back in, and still use the up key to get to an earlier command.

Another, even more useful feature of the Bash shell is that you can refer to an earlier command using a type of short-hand. If you type an exclamation point (!), which is known in UNIX/Linux parlance as "bang", and then the first few characters of the command and press return, the shell will search back through commands until it finds the first one that matches the characters you entered and execute it. Below is an example:

Figure 14. Example of Using History

[rben@Prot linux-help-how-to]$ ls
ar01s02.html  ar01s06.html  ar01s10.html            terminalwindow.png
ar01s03.html  ar01s07.html  ar01s11.html
ar01s04.html  ar01s08.html  Getting Help HOWTO.xml
ar01s05.html  ar01s09.html  index.html
[rben@Prot linux-help-how-to]$ history 10
 1003  ls *.xml
 1004  mv Getting\ Help\ HOWTO.xml linux-help-how-to/
 1005  cls
 1006  ls
 1007  cd linux-help-how-to/
 1008  ls
 1009  history
 1010  history 10
 1011  ls
 1012  history 10
[rben@Prot linux-help-how-to]$ !ls
ar01s02.html  ar01s06.html  ar01s10.html            terminalwindow.png
ar01s03.html  ar01s07.html  ar01s11.html
ar01s04.html  ar01s08.html  Getting Help HOWTO.xml
ar01s05.html  ar01s09.html  index.html
[rben@Prot linux-help-how-to]$

As you can see in the example above, I first use ls to give me a listing of the files in the directory, then I used the command history 10 to show me the last 10 commands I'd used. When I then used the command !ls the shell found the previous ls command and ran it. This is a trivial example and it would have been easier for me to type in just ls again, but if I'd just typed in a long complicated command, it would have saved me a lot of typing and the chance of making an error typing in the long command again.

In addition to the history related functions, Bash also allows you to edit the command line using the right and left keys. Using those keys you can move through the command and then use the delete or backspace keys to erase characters and type in changes. Used in combination with the command history and the up key, you can easily recall and modify complicated command lines and edit them.

One final bit of useful information about commands is how to start a command that runs in the background. That is done using the ampersand after the command. For instance, the command mozilla& tells the Bash shell to run the command mozilla in the background. If you started mozilla the normal way, just typing the command on the command line, mozilla would start up, but you wouldn't be able to do anything else with that terminal window until you closed Mozilla. If you accidentally closed the terminal window, it would also close Mozilla. When a process is started in the background, the terminal window is immediatly available for more commands and if it's close accidentally, the background application continues to run until it shuts down nomally.