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.
- Complete commands, file names and options with Tab.
- Find and rerun earlier commands with the history list,
!!,!Nand!string, and reuse arguments with Alt+.. - Move around and edit the command line with the Ctrl shortcuts, and search the history with Ctrl+R.
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 ~]$ passwdIt 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 --passwordThe 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 historyThe history is saved to ~/.bash_history when you log out, so it survives from one session to the next.
Rerun a command
| Type | Runs |
|---|---|
| ↑ / ↓ | Step back and forward through the history; edit the command, then press Enter |
!! | The previous command again |
!N | Command number N from history |
!-N | The command N commands ago (!-1 is the same as !!) |
!string | The 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:30Reuse 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).
| Shortcut | Does |
|---|---|
| Ctrl+A / Ctrl+E | Jump to the start / end of the line |
| Ctrl+← / Ctrl+→ | Jump one word left / right |
| Ctrl+U | Delete from the cursor back to the start of the line |
| Ctrl+K | Delete from the cursor to the end of the line |
| Ctrl+R | Search the history: type part of a command, press Ctrl+R again for older matches, Enter to run, Esc to edit it first |
| Ctrl+C | Abandon the line (or stop a running command) |
| Ctrl+L | Clear 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
- You type ls /etc/pas and press Tab. Nothing is completed, and a second Tab lists passwd and passwd-. Why?
- Your history shows 26 as ls /etc. What does !26 do?
- Which runs the most recent command that started with tail?
- You have just run wc -l /var/log/secure. How do you insert /var/log/secure into a new command without typing it?
- The cursor is in the middle of a long command. Which shortcut deletes everything from the cursor to the end of the line?
- You remember running a long ssh command yesterday but not its exact options. What is the quickest way to find it?