Chapter 2: Command-line essentials

Work faster with completion and history

Experienced administrators don't type faster; they type less. Bash completes names for you, remembers every command you run, and lets you edit a command line without retyping it. These few keys save thousands of keystrokes a day, and prevent typing mistakes in long paths.

Tab completion

Type the start of a command or file name and press Tab. If only one thing matches, Bash completes it. If several match, the first Tab completes as far as they agree and a second Tab lists them:

[student@workstation ~]$ pas Tab Tab
passmass  passwd    paste
[student@workstation ~]$ passw Tab
[student@workstation ~]$ passwd

It works for file names anywhere on the line:

[student@workstation ~]$ ls /etc/pas Tab
[student@workstation ~]$ ls /etc/passwd Tab Tab
passwd   passwd-

And, for most commands, for their options:

[root@servera ~]# useradd -- Tab Tab
--badname               --help                  --prefix
--base-dir              --home-dir              --root
--btrfs-subvolume-home  --inactive              --selinux-user
--comment               --key                   --shell
--create-home           --no-create-home        --skel
--defaults              --no-log-init           --system
--expiredate            --non-unique            --uid
--gid                   --no-user-group         --user-group
--groups                --password

The command history

Bash remembers the commands you run. The history command lists them with numbers:

[student@workstation ~]$ history
    1  date +%R
    2  file /etc/passwd
    3  wc -l /etc/passwd
    4  tail -n 3 /etc/passwd
    5  history

The history is saved to ~/.bash_history when you log out, so it survives from one session to the next.

Rerun a command

TypeRuns
↑ / ↓Step back and forward through the history; edit the command, then press Enter
!!The previous command again
!NCommand number N from history
!-NThe command N commands ago (!-1 is the same as !!)
!stringThe most recent command that starts with string

Bash prints the expanded command before running it, so you can see exactly what ran:

[student@workstation ~]$ !wc
wc -l /etc/passwd
20 /etc/passwd
[student@workstation ~]$ !1
date +%R
09:30

Reuse the last argument

Alt+. (or Esc then .) inserts the last word of the previous command at the cursor. Press it again to step back to the command before that. It is the fastest way to run several commands on the same file:

[student@workstation ~]$ file /etc/redhat-release
/etc/redhat-release: symbolic link to rocky-release
[student@workstation ~]$ cat Alt+.
[student@workstation ~]$ cat /etc/redhat-release
Rocky Linux release 9.8 (Blue Onyx)

Edit the command line

The arrow keys move the cursor one character at a time. These shortcuts move further, and work in nearly every Linux program that reads a line of text. Select one to see what it does to the line:

What each editing shortcut does: The cursor (amber) sits after -n 20. Select a shortcut to see where the cursor jumps, or what is deleted (red).

ShortcutDoes
Ctrl+A / Ctrl+EJump to the start / end of the line
Ctrl+← / Ctrl+→Jump one word left / right
Ctrl+UDelete from the cursor back to the start of the line
Ctrl+KDelete from the cursor to the end of the line
Ctrl+RSearch the history: type part of a command, press Ctrl+R again for older matches, Enter to run, Esc to edit it first
Ctrl+CAbandon the line (or stop a running command)
Ctrl+LClear the screen, keeping the line you are typing

A typical combination: you ran tail zcat and want the last 20 lines instead. Press ↑, Ctrl+A, Ctrl+→, type -n 20, and press Enter. Six keystrokes and the option, instead of retyping the line.

Try it

This practice terminal keeps a history and understands !!, !N, !string, Alt+., Ctrl+R and the editing keys, exactly like Bash. The tasks practise them in order.

Check your understanding