Terminal Basics: Command Anatomy, Help Pages, History and Shortcuts

Beginner
12 min

Terminal Basics: Command Anatomy, Help Pages, History and Shortcuts

Every Linux command follows the same grammar, and once you can read that grammar you can use tools you have never seen before by consulting their manual. This lesson covers the structure of a command line, the difference between the terminal and the shell, how to look up help, how to reuse your command history, and the keyboard shortcuts that make experienced users look fast.

Terminal vs Shell

The terminal is the window that draws text and captures your keystrokes. The shell is the program running inside it that interprets what you type. On most Linux systems the shell is bash (Bourne Again Shell); macOS defaults to zsh, and many developers install zsh or fish on Linux too. Everything in this course is written for bash, and nearly all of it works unchanged in zsh.

The prompt is the shell asking for input. A typical one looks like user@hostname:~$. The ~ is your current directory (home), and the trailing $ means you are a normal user; a # means you are root.

Anatomy of a Command

A command line has three parts: the command name, zero or more options (also called flags or switches) that modify behaviour, and zero or more arguments that name what to act on.

bash
ls -l -a /var/log # ^^ ^^ ^^^^^^^^ # options argument

Conventions worth memorising:

  • Short options are one dash and one letter: -l, -a. They can be combined: -la.
  • Long options are two dashes and a word: --all, --human-readable. They are easier to read in scripts.
  • Some options take a value: -n 5 or --lines=5.
  • -- marks the end of options, so rm -- -weird-name deletes a file whose name starts with a dash.
  • Words are separated by spaces. Filenames with spaces must be quoted: cat "My Notes.txt".

Getting Help Without Leaving the Terminal

bash
man ls # the manual page: press / to search, n for next match, q to quit ls --help # a compact summary of options (GNU tools) man -k compress # search manual titles for a keyword (same as apropos) type cd # "cd is a shell builtin" which python3 # /usr/bin/python3 — where the program lives help cd # help for bash builtins such as cd, echo, export

Manual pages are grouped into numbered sections. Section 1 is user commands, 5 is file formats and 8 is administration commands. man 5 crontab opens the crontab file format rather than the crontab command.

The SYNOPSIS line at the top of a man page uses brackets for optional parts and ... for repeatable ones. cp [OPTION]... SOURCE... DIRECTORY tells you that cp takes optional flags, one or more sources and a final destination directory.

Command History

Bash records what you type in ~/.bash_history. Reusing it saves enormous amounts of typing.

| Action | Keys or command | |---|---| | Show history | history | | Re-run the previous command | !! | | Re-run command number 42 | !42 | | Re-run the last command starting with ssh | !ssh | | Last argument of the previous command | !$ or Alt+. | | Search history interactively | Ctrl+R, then type part of the command | | Delete a line from history | history -d 42 |

A common pattern is mkdir ~/projects/api followed by cd !$, which changes into the directory you just created.

Keyboard Shortcuts That Matter

These work in bash and zsh and are worth learning in the first week.

bash
Tab # complete a command, filename or path; press twice to list choices Ctrl+C # cancel the running command Ctrl+D # end of input; on an empty prompt it logs you out Ctrl+L # clear the screen (same as the clear command) Ctrl+A / Ctrl+E # jump to the start / end of the line Ctrl+U # delete from the cursor to the start of the line Ctrl+K # delete from the cursor to the end of the line Ctrl+W # delete the word before the cursor Ctrl+Z # suspend the running program (covered in the jobs lesson)

Tab completion is the single biggest productivity gain. Type cd /va and press Tab; bash expands it to /var/. It also completes command names, options for many tools, hostnames after ssh, and package names after apt install.

Running Several Commands

You can put more than one command on a line. ; runs them in sequence regardless of success, && runs the second only if the first succeeded, and || runs the second only if the first failed. The pipes lesson covers these in depth, but you will see them everywhere from now on.

bash
sudo apt update && sudo apt upgrade -y mkdir build; cd build ping -c 1 example.com || echo "network is down"

Common Mistakes

  • Typing a command and forgetting the space before options: ls-l is not a command.
  • Using man on a shell builtin such as cd and getting the general bash page; use help cd instead.
  • Pressing Ctrl+S by accident, which freezes terminal output. Press Ctrl+Q to resume.
  • Ignoring "command not found": it usually means the program is not installed or not on your PATH, which a later lesson explains.
Quick Quiz
Question 1 of 3

In `grep -in "error" app.log`, which part is the argument?

Key Takeaways

  • A command line is command [options] [arguments]; short options combine, long options are readable.
  • man, --help, type and help answer almost every "how do I use this" question offline.
  • History expansion (!!, !$) and Ctrl+R remove most repetitive typing.
  • Tab completion works for commands, paths, hostnames and package names.
  • && chains commands safely; ; runs them unconditionally.

Next lesson: The Linux Filesystem Hierarchy — learn what lives in /etc, /var, /usr and /home and why the layout is the same on every distribution.

Terminal Basics: Command Anatomy, Help Pages, History and Shortcuts - Linux & Command Line | CodeYourCraft | CodeYourCraft