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

Cheat sheet

Commands

CommandDoes
whoami / hostnameWho and where you are
date, date +%RThe date and time; +FORMAT picks the parts
passwdChange your password
file FILEWhat kind of file it really is
cat FILEPrint the whole file
less FILEPage through a file: Space, b, /search, q
head -n N FILE / tail -n N FILEFirst / last N lines (default 10)
wc FILE, wc -l FILELines, words, bytes; -l lines only
ssh user@hostLog in to another machine
ssh host commandRun one command there and see its output here
sudo -iRoot shell (asks for your password)
historyNumbered list of earlier commands
COMMAND --helpShort usage summary

Keys

KeysDoes
Tab, Tab TabComplete; list the choices
↑ / ↓Step through the history
!!, !N, !-N, !stringRerun the last / number N / N ago / last starting with string
Alt+.Insert the previous command's last word
Ctrl+A / EStart / end of the line
Ctrl+← / →One word left / right
Ctrl+U / KDelete to the start / end
Ctrl+RSearch the history backwards
Ctrl+CAbandon the line, or stop a command
Ctrl+DEnd of input: logs out of a shell
Ctrl+LClear 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 date

Flashcards

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.