⌨️

Level 2 of 24

Command Line

The shell, navigation, environment variables, and how command syntax actually works.

The shell is the primary interface for everything you'll do as a Linux administrator — GUIs are the exception on servers, not the rule. This level builds real fluency with the shell itself: navigation, environment variables, and how a command line is actually parsed, not just a list of commands to memorize.

Prerequisites

  • • Level 1: Linux Fundamentals — specifically, a working Ubuntu Server VM you can SSH into.

By the end of this level, you can

  • ✓ Navigate the filesystem confidently from the command line
  • ✓ Read and explain command syntax: commands, options, and arguments
  • ✓ Understand and use environment variables, especially $PATH
  • ✓ Explain shell expansion and command exit status

The shell and your first commands

pwd, ls, cd, and the difference between the terminal, the shell, and a command. · 9 min

A terminal (or terminal emulator) is the program that gives you a text window and handles keyboard input and screen output. A shell is the program running inside that terminal that reads what you type, interprets it, and runs commands — bash is the default shell on the overwhelming majority of Linux distributions, though zsh and others exist. When you SSH into a server, you are connecting directly to a shell session — there is no terminal emulator involved on the remote end in the GUI sense, just a shell reading from and writing to your SSH connection.

Three commands answer the three questions you ask constantly when working blind on a remote server: `pwd` (print working directory) tells you where you are, `ls` lists what's in the current directory, and `cd` changes into another directory. These sound trivial, but on an unfamiliar server with no GUI, "where am I and what's here" is the very first thing you establish before doing anything else — treat it as a habit, not a formality.

A few commands round out basic orientation: `whoami` and `id` tell you who you're logged in as and what groups you belong to (critical before you assume you have permission to do something), `hostname` tells you which machine you're actually on (easy to forget when you have six SSH sessions open in different tabs), and `history` shows your recent command history — genuinely useful for reconstructing "what did I just run that broke this".

CommandPurposeExample
pwdPrint the current working directory—
lsList directory contentsls -la → long format, including hidden files
cdChange directorycd /var/log cd .. cd ~ cd - (previous directory)
whoamiPrint the current username—
idPrint user and group IDs for the current (or a named) user—
hostnamePrint the system's hostname—
historyShow recent command history for the current shell session—
clearClear the terminal screen (doesn't affect history or running processes)—

A typical orientation sequence on an unfamiliar server

$ whoami
deploy
$ hostname
web-prod-03
$ pwd
/home/deploy
$ ls -la
total 24
drwxr-xr-x 3 deploy deploy 4096 Sep 19 10:02 .
drwxr-xr-x 5 root   root   4096 Aug  2 09:11 ..
-rw-r--r-- 1 deploy deploy  220 Aug  2 09:11 .bash_logout

Takeaway: Before doing anything on an unfamiliar server, run whoami, hostname, and pwd — cheap habits that prevent a surprising number of "wrong machine" or "wrong permissions" mistakes.

Command syntax: commands, options, and arguments

Reading `ls -la /var/log` correctly, and why option order and combination rules matter. · 8 min

Every shell command follows roughly the same shape: `command [options] [arguments]`. The command is the program to run. Options (also called flags or switches) modify how it behaves, conventionally prefixed with a single dash for short form (`-l`) or double dash for long form (`--all`) — and many commands let you combine short options together, so `ls -l -a` and `ls -la` are equivalent. Arguments are the actual targets the command operates on — a filename, a directory, a username.

This structure is a convention, not a hard rule enforced by the shell itself — the shell just splits your input on whitespace and hands the pieces to the program, which decides for itself what counts as an option versus an argument. That's why most, but not all, commands follow this pattern consistently, and why reading a command's `--help` output or man page matters more than pattern-matching from memory when you hit an unfamiliar tool.

Two commands you'll use constantly to learn any other command's syntax: `man <command>` opens the full manual page (press `q` to quit, `/searchterm` then Enter to search within it), and `<command> --help` prints a shorter usage summary directly to the terminal. When you're unsure whether a flag exists or what it does, check one of these before guessing — guessing with an unfamiliar flag on a destructive command is exactly how production incidents start.

CommandPurposeExample
manOpen the manual page for a commandman ls
--helpPrint short usage help (supported by most, not all, commands)ls --help
whichShow the full path of the executable that would run for a given command namewhich python3
typeShow whether a name is a shell builtin, alias, or external commandtype cd

Common Mistakes

  • ⚠ Assuming every command uses GNU-style `--long-options` — some older or minimal-system utilities only support single-letter flags
  • ⚠ Combining options that don't make sense together and getting confusing output, instead of checking `man` first
  • ⚠ Running an unfamiliar flag on a destructive command (`rm`, `dd`) without checking what it does — always check first

Takeaway: `man <command>` and `<command> --help` are not beginner crutches — experienced administrators use them constantly, specifically to avoid guessing on unfamiliar flags.

Environment variables and $PATH

Why "command not found" usually means a $PATH problem, not a missing program. · 10 min

An environment variable is a named value available to your shell and every program it launches — think of it as configuration that travels with your session rather than being hard-coded into any single program. You reference a variable's value with a `$` prefix (`$HOME`), and set one with `export NAME=value` (in bash). Common ones you'll see constantly: `$HOME` (your home directory), `$USER` (your username), `$SHELL` (your default shell's path), and `$PWD` (current working directory, kept in sync automatically by the shell).

`$PATH` deserves special attention because it explains one of the most common early confusions: when you type a command name like `ls` without a full path, the shell doesn't search your entire filesystem for it — it only checks the directories listed in `$PATH`, in order, separated by colons, and runs the first matching executable it finds. This is exactly why "command not found" for a program you're certain is installed usually means the directory containing it isn't in your `$PATH`, not that the program doesn't exist — and why running an executable in your current directory needs an explicit `./` prefix (`./myscript.sh`), since the current directory is deliberately not included in `$PATH` by default for security reasons (imagine a malicious file named `ls` sitting in a directory you `cd` into).

Variables set with plain `VAR=value` exist only in your current shell; `export VAR=value` makes that variable available to any program the shell launches afterward (its "child processes"), which is why you'll see `export` used for anything a script or program downstream needs to read, and plain assignment for values only the interactive shell itself needs.

CommandPurposeExample
echo $PATHPrint the current $PATH value—
export VAR=valueSet an environment variable and make it available to child processes—
envList all environment variables currently set—
unset VARRemove an environment variable from the current shell—

Diagnosing a "command not found" via $PATH

$ mytool
bash: mytool: command not found

$ echo $PATH
/usr/local/bin:/usr/bin:/bin

$ find / -name mytool 2>/dev/null
/opt/mytool/bin/mytool

# Fix: add it to PATH for this session
$ export PATH="$PATH:/opt/mytool/bin"
$ mytool
# now works

Quick Check

You installed a program, but running its name gives "command not found". What is the most likely cause?

Takeaway: "Command not found" for something you know is installed is almost always a $PATH problem — check `echo $PATH` and where the program actually lives before assuming it's broken.

Sponsor / Advertisement