Chapter 2: Command-line essentials
Linux command line cheat sheet
The chapter on one page: what to remember, the commands and keys to have at your fingertips, and flashcards for quick revision.
The chapter in eight sentences
- Linux is the kernel; a distribution such as RHEL packages it with thousands of tools into a complete, supported operating system. Rocky Linux rebuilds RHEL from the same source.
- The shell (Bash) reads your commands, runs programs and shows their output in a terminal.
- The prompt shows the user, the host and the current directory, and ends in
$for an ordinary user or#for root. - A command line is a command, then options (
-n 3,--all), then arguments (what to work on). ssh user@hostlogs you in to another machine; the first connection asks you to confirm its host key, and a changed key is a warning.exitor Ctrl+D logs out.filetells you what a file really is;cat,less,headandtailshow it in whole or in part, andwccounts it.- Tab completes commands, names and options; the history reruns commands with the arrows,
!!,!Nand!string, and Alt+. reuses the last argument. - Ctrl+A/E/U/K/R move, delete and search on the command line, so you edit instead of retyping.
Cheat sheet
Commands
| Command | Does |
|---|---|
whoami / hostname | Who and where you are |
date, date +%R | The date and time; +FORMAT picks the parts |
passwd | Change your password |
file FILE | What kind of file it really is |
cat FILE | Print the whole file |
less FILE | Page through a file: Space, b, /search, q |
head -n N FILE / tail -n N FILE | First / last N lines (default 10) |
wc FILE, wc -l FILE | Lines, words, bytes; -l lines only |
ssh user@host | Log in to another machine |
ssh host command | Run one command there and see its output here |
sudo -i | Root shell (asks for your password) |
history | Numbered list of earlier commands |
COMMAND --help | Short usage summary |
Keys
| Keys | Does |
|---|---|
| Tab, Tab Tab | Complete; list the choices |
| ↑ / ↓ | Step through the history |
!!, !N, !-N, !string | Rerun the last / number N / N ago / last starting with string |
| Alt+. | Insert the previous command's last word |
| Ctrl+A / E | Start / end of the line |
| Ctrl+← / → | One word left / right |
| Ctrl+U / K | Delete to the start / end |
| Ctrl+R | Search the history backwards |
| Ctrl+C | Abandon the line, or stop a command |
| Ctrl+D | End of input: logs out of a shell |
| Ctrl+L | Clear the screen |
Syntax
command [options] [arguments]
cmd1 ; cmd2 two commands on one line
long command \ one command on several lines (the next line continues it)
continued
date +%Y-%m-%d an argument beginning with + is a format for dateFlashcards
- What is the difference between the kernel and a distribution?
- The **kernel** manages the hardware for every program. A **distribution** packages the kernel with tools, an installer and updates into a complete system.
- What does `
Linux command line Cheat Sheet (RHCSA) or `#` at the end of the prompt mean? - `
Linux command line Cheat Sheet (RHCSA) an ordinary user; `#` **root**, who can change anything. - Name the three kinds of part on a command line.
- The **command**, its **options** (start with a dash) and its **arguments** (what it works on).
- What does SSH do with a server's host key on the first connection?
- Shows the fingerprint, and if you answer yes, **saves it** in `~/.ssh/known_hosts` to check silently on every later connection.
- Two ways to log out of a shell?
- `exit` or **Ctrl+D**.
- Which command tells you what kind of file something is?
- `file`. Linux ignores file name extensions.
- Last 20 lines of a file?
- `tail -n 20 FILE`
- Count only the lines of a file?
- `wc -l FILE`
- Rerun the previous command?
- `!!`, or the up arrow then Enter.
- Rerun the latest command that started with `ssh`?
- `!ssh`, or Ctrl+R then type ssh to see it first.
- Insert the previous command's last argument?
- **Alt+.** (or Esc then .); repeat to go further back.
- Delete from the cursor to the end of the line?
- **Ctrl+K**. Ctrl+U deletes back to the start.