History, completion and shortcuts

Video 109 of 140 in Virtualization Foundations, in the unit “The shell and the filesystem tree”. 2:39, with English and Hindi audio.

Typing long names again and again is slow, and every typo costs another try. The shell can do much of that work for you. It keeps a history of the commands you ran, it can finish a long file name when you press Tab, and it can stop a command that is still running. This article shows the keys that save time, how to read the history, and one risk to avoid when you type commands.

What you will learn

Why the shell remembers your commands

An engineer on a Harbor Point Logistics server checks the same long log path every few minutes. Harbor Point is a fictional logistics firm, used for training examples. Typing that long path again is slow, and each typo costs another try. The shell keeps a history of the commands you run, and it can finish names for you, so a few keys save a lot of typing.

Completing a name with Tab

Type the opening letters of a file or folder, then press Tab. When the name is unique, the shell fills in the rest. Press Tab twice to see the options when several names match.

Type the start of a path, then press Tab
ls /var/lo

Note: Tab completes the path to /var/log/, with a slash at the end.

Walking through history with the arrows and Ctrl and R

The up and down arrows walk through the commands you ran before, so you can run one again without retyping it. Ctrl and R starts a search through your history. Type a few letters, and the shell finds the newest match.

Stopping a command and reading recent history

Press Ctrl and C to stop a running command. The sleep command below waits for five minutes, and Ctrl and C ends that wait early.

A command that waits for five minutes
sleep 300

The history command lists the commands you ran recently. Give it a count, so that it shows only the few most recent ones.

Show the three most recent commands
history | tail -n 3

Keep secrets out of the history

Anything typed as an argument can be kept in the history. That is convenient for repeating a command, but it is a risk for secrets. Never type a password on the command line, because the history can keep it.

Try it yourself

You need a Linux terminal. Use Tab, Ctrl and C and the history in turn, and watch what the shell does at each step.

  1. Type the start of a path, then press Tab to let the shell finish the name.

    ls /var/lo

    What you should see: Tab completes the path to /var/log/.

  2. Start a command that waits for five minutes. Then press Ctrl and C to stop the wait.

    sleep 300

    What you should see: Ctrl and C stops the wait, and the prompt returns at once.

  3. List the three most recent commands from your history.

    history | tail -n 3

    What you should see: The shell prints the three most recent commands, each with its number.

Terms

History
The list of commands the shell keeps from your earlier lines, so you can run one again.
Tab completion
The shell finishing a file or folder name for you when you press Tab and the match is unique.

Check yourself

What does Tab do when you type the start of a name?

It completes the name when only one match exists. If several names match, pressing Tab twice shows the options.

How do you find an older command quickly?

Use the up arrow to walk back through the history, or press Ctrl and R and type a few letters to find the newest match.

Why should you avoid typing a password as part of a command?

Anything typed as an argument can be kept in the history, so the password could be stored there.

Previous: Environment variables and PATH. Next: Users and groups: the files behind them.