Command History and Tab Completion

It doesn't take long before the thought of typing the same command over and over becomes unappealing, at best.

Linux users don't feel any differently about that, either. In Linux, since you can string together commands at the shell prompt, one minor typo in a couple lines of a command could mean that all that typing was in vain.

So there's a solution: It's called command-line history. By scrolling with the up and down arrow keys, we can find plenty of our previously typed commands -- including the ones with typos.

Let's try it by taking a look again at sneakers.txt. The first time, however, at the shell prompt, we'll type:

cat sneakrs.txt

Nothing happens, of course, because there is no sneakrs.txt file. No problem. We'll just use the up-arrow key to bring back the command, then use the left-arrow key to get to the point where we missed the "e." Insert the letter and press Enter again.

Voila! We now see the contents of sneakers.txt.

By default up to 500 commands can be stored in the bash command-line history file.

TipLooking at the environment variable

By typing the env command at a shell prompt, we can see the environment variable that controls the size of the command-line history. The line which reads, HISTFILESIZE=500 shows the number of commands that bash will store.

The command-line history is actually kept in a file, called .bash_history in our login directory. We can read it in a number of ways: by using pico, cat, less, more, and others.

Be prepared, though: the file can be pretty long.

Let's read it with more:

more .bash_history

To move forward a screen, press Space; to move back a screen, press B; to quit, press Q.

TipLocating a previously used command

Want to find a command in your history file without having to keep hitting the arrow keys or page through the history file? Use grep, a powerful search utility. Here's how you can quickly find a previously used command: Let's say you're searching for the command that was something like cat sneak-something. You've used the command and think it might be in your history file. At the shell prompt, then, type

history | grep sneak

In addition to the command you've just typed, you'll see your sought-after command, as well, because grep searched through your history file for every instance in which the word "sneak" appeared. You can learn more about grep later in this chapter, when we discuss tools which can help you read files.

Another time-saving tool is known as command completion. If you type part of a file, command or pathname then press the Tab key, bash will present you with either the remaining portion of a the file/path, or a beep. If you get a beep, just press Tab again to obtain a list of the files/paths that match what's been typed so far.

For example, if you forget the command updatedb, but remember a portion of the command, you can su to root, then at the shell prompt, type up, press the Tab key twice and you'll see a list of possible completions, including updatedb and uptime. By adding the letter "d" to up and pressing Tab again, your command is completed for you.

So even if the machine is turned off at the end of the day, it's not too hard to update the slocate database: The chances are good that the command will be stored in the command-line history or can be completed with command completion (as long as you remember the start of the pathname for the command).