Command history - Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

Command history

Every time you type a command at the prompt, it’s remembered. A rolling list of 500 commands are recorded, in fact, and you can see a list of them by typing history at the command prompt.

TIP If you only want to look at the last 10 commands, you could type history 10. For the last 20, type history 20, and so on.

Commands are remembered so you can reuse them. You can cycle through the history list, from the most-recently-typed command up, using the up/down cursor keys (Ctrl+P and Ctrl+N do the same thing).

Alternatively, you can reuse any command in the history list by typing its number (as shown by the history command), preceded by an exclamation mark. If you want a view a file listing of the Documents folder, and ls Documentsis command #480 in the list, you could type !480.

Here’s another great trick: say you’ve just typed a long file copy (cp) command, and want to use it again. Typing !cp will find the last line in your history list that begins with cp and reuse it.

There are other neat history shortcuts too. Typing two bangs (!!) will cause the last command you typed to be reused. Typing !? and then part of a recently-typed command will cause bash to autocomplete using the nearest match it can find in the history. For example, typing !?upd on my system caused bash to reuse the sudo updatedb command, that was #471 in my computer’s history list.

Hitting Ctrl+R lets you interactively search the history—just start typing the first part of the command, and bash will fill in the rest, guessed from your history. If bash autocompletes with an entry from your history that’s incorrect, hit Ctrl+R again to step further back through your history to see another match until you find the one you’re looking for.

There’s a lot more to the command history function provided by bash. You’re advised to read the history man page by typing man history.

TIP If ever you want to clear your history, just type history –c. Incidentally, the history data is kept in a simple text file called .bash_history in your /home folder.

Job management

Thanks to the astonishing power of modern computers, very few commands take a long time to complete.

NOTE Bear in mind that modern computers are like gods compared to the room-sized computers that Unix was first developed on in the 1970s and 80s. Because of this, some of bash’s features can seem a little redundant in our modern day and age, but it’s surprising how often they come in useful.

However, even though most commands complete in the blink of an eye, it’s often useful to be able to run a particular command in the background, in order to get on with something else.

For example, the updatedb command updates the database of files used by the locate file search command. You can run it from the command-line by typing sudo updatedb. If you do so, the prompt will be tied up for up to a few minutes, during which you won’t be able to work unless you open a new terminal window.

To avoid this, you can set the command to run in the background after starting by adding an ampersand (&) character to the end:

sudo updatedb &

You’ll be told the job number (in square brackets), and can switch to the new job by typing fg.

Alternatively, you can start the command as usual and hit Ctrl+Z. This will then put the command to the background, returning control of the prompt to you. Again, you can switch back by typing fg.

An example of how this might be useful might be if you’re editing a file using a text editor. By hitting Ctrl+Z, you can temporarily abandon the text editor to do something else, and type fg to switch back to it when you need to.

Many jobs can be started as background tasks, and you can list them using the jobs command. You can switch to individual jobs by typing a percentage symbol and the job number—for example %2 will switch to job #2 in the list.

Managing processes

One of the commonest command-line tasks is clearing-up programs that have crashed, something known as managing processes.

On a technical level, Linux refers to currently running programs as processes.

NOTE Some programs start more than one process, so perhaps a better definition of a process is that it is all or part of a program, and not necessarily an entire program.

You can see a constantly updating list of processes by typing the top command in a terminal window (hitting Q will quit when you’re done).

As with files and folders, all processes are “owned” by a user. Only the individual who owns a process can terminate it, although the root user has the power to terminate any process.

The majority of processes listed in top are started at boot-up. These provide essential background services, such as the GUI. Such processes are usually owned by the root user. This protects them from interference by ordinary users.

All processes are numbered. The number is known as the Process ID, or PID, and this is listed on the left of each entry in the top program list. To force a process to quit—known as killing the process—hit K and type the PID. You’ll be asked what signal you wish to send. Hitting Enter selects the default (15). This is fine in most cases.

You can also use the ps command to list processes at the command-line, and find-out process IDs. Normally the a, u and x command options are used with ps, and cause the command to return a full list of all processes complete with their names.

Additionally, the output is usually “piped” into the grep command to search for the program you’re interested in. I discuss piping in the Advanced Bash Techniques section later, but for now it’s enough to know that piping “sends” the output of one command into another.

Let’s look at an example. Although Firefox very rarely crashes, let’s say that you’ve visited a website that’s caused it to lock-up. All attempts to quit the program in the usual way no longer work.

The following command will search the list of running processes and return the PID of Firefox:

ps aux|grep firefox

The PID is the first number listed on each line in the results, and on my test PC the PID for Firefox was 15994. Knowing this, I was able to use the kill command, as follows:

kill 15994

This caused Firefox to instantly quit. Bear in mind that, if you kill a program in which you are editing data, you won’t be prompted to save it first. The kill command shows no mercy!

Another method of killing a program is to use the killall command. This lets you specify a command name, rather than the PID. So, to terminate Firefox, you could type the following:

killall firefox

The downside of killall is that you need to know the program’s command name. ps aux can be used to discover this. killall also, as its name suggests, kills all examples of the process with the specified name.

If you need to kill a root-owned process, simply precede kill or killall with sudo. Be very careful killing root-owned processes, however, because they tend to be related to the running of background services. Additionally, background service processes frequently spawn their own set of processes, and killing the parent process will in most cases also kill its child processes—often with disastrous results.