LINUX · LPI LINUX ESSENTIALS · CHAPTER 3

Finding Your Way on a Linux System

Your first steps in the terminal: commands, help, paths and files.

6 min readDomain 012 · Command Line BasicsLevel: beginner

From point A to point B

Point A: you open a terminal and see a blinking cursor without knowing what to type. Point B: by the end of this chapter you will understand how a command is built, use variables and the PATH variable, find help without leaving the terminal, move around the file system with absolute and relative paths, and create, copy, move and delete files safely.

Together with the next one, this is the most heavily weighted block of the exam, and the skill you will use most in Docker and Kubernetes.

The command interpreter

The command interpreter or shell is the program that reads what you type and asks the operating system to do it. You will also hear it called the terminal or the command line, although technically the terminal is the window and the shell is the program running inside it.

There are several shells: Bash (the most widespread and the one on the exam), sh, zsh (the default on macOS), fish or tcsh.

echo $SHELL        # your login shell
pwd                # which directory you are in (print working directory)
clear              # clears the screen (or Ctrl+L)

Anatomy of a command

Anatomy of a command line In ls -l -a /home, ls is the command, -l and -a are options that change how it behaves, and /home is the argument it acts on. Options can be combined as -la. COMMAND · OPTIONS · ARGUMENTS $ ls -l -a /home commandwhat to run optionshow · same as -la argumenton what

Command, options and arguments. Diagram: The House of Routing.

Short options take one dash and can be grouped (-l -a is the same as -la); long options take two (--all). The convention is to write the options first and then the arguments, although many GNU commands accept a different order.

Case matters. Linux tells Report.txt and report.txt apart: they are two different files.

echo and quotes

echo "Hello world"
echo -e "Hello\tworld"     # -e interprets escape sequences: \t tab
echo -e "Hello\nworld"     # \n newline

Quotes change how the shell treats what is inside them:

QuotesBehaviorExample
Double "..."Expand variablesecho "Your shell is $SHELL" → Your shell is /bin/bash
Single '...'Everything is literalecho 'Your shell is $SHELL' → Your shell is $SHELL
Backslash \Escapes a single characterecho "Price: \$5" → Price: $5

Variables, export and PATH

A variable stores a value. You create it with no spaces around the = and read it with $:

course=Linux             # local shell variable
echo $course
export course            # turns it into an environment variable: programs you launch inherit it
unset course             # removes it

env                     # lists environment variables
set                     # lists all shell variables and functions
env LANG=C date         # runs a command with a variable changed only for that command

PATH: where the shell looks for commands

PATH is a list of directories separated by colons. When you type a command that is not built into the shell, Bash looks for it in those directories, in order.

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

export PATH="$PATH:$HOME/scripts"    # adds your scripts folder

That change lasts only for the session. To make it persistent, add it to your shell's startup file (~/.bashrc in Bash, ~/.zshrc in zsh) and reload it:

echo 'export PATH="$PATH:$HOME/scripts"' >> ~/.bashrc
source ~/.bashrc
Notice the single quotes. With double quotes, $PATH would expand right then and you would save a fixed copy of the path. With single quotes the text is saved as is and expands every time you open a session. Always use >> (append): a single > would overwrite your .bashrc.

The shebang

The first line of a script states which interpreter should run it:

#!/bin/bash
date
echo "Hello from my first script"

With chmod +x and the folder in PATH, you run it by name from anywhere. Scripts are covered in depth in the next chapter.

Knowing what a command is: type, which and uname

type cd        # cd is a shell builtin       → built into Bash
type ls        # ls is aliased to 'ls --color=auto'
type cp        # cp is /usr/bin/cp            → external program
which cp       # /usr/bin/cp                   → path of the executable in PATH

uname -a                 # kernel, version and architecture
cat /etc/os-release      # distribution and version

type tells you whether something is a builtin, an alias, a function or an external program; which only searches for programs in PATH.

Getting help without leaving the terminal

ls --help          # quick help (or -h in many programs)
man ls             # full manual: q to quit, /word to search
man 5 passwd       # section 5: the format of the /etc/passwd file
man -f passwd      # one-line description (same as whatis)
man -k password    # searches by keyword (same as apropos)

Manual pages are organized into sections. The most used are 1 (user commands), 5 (file formats) and 8 (administration commands). That is why man passwd and man 5 passwd show different things.

Other useful sources: info, with documentation you navigate through links; the /usr/share/doc/ folder, with documentation for each package; and to find files, find (walks the disk in real time) or locate (queries a database that is refreshed with updatedb).

find ~ -name "*.txt"           # .txt files in your home directory
whereis bash                   # binary, source and manual for bash
Exam version 2.0. The new syllabus gives more weight to --help and -h, adds find and drops info and locate. Learn them anyway: you will find them on many systems.

Directories, paths and ls

The entire file system hangs from a single root directory, /. Your home directory is usually /home/your_user and is abbreviated as ~.

Absolute and relative paths The tree starts at the root, slash. From the root it goes to home, then jesus, then scripts. The absolute path /home/jesus/scripts starts at the root and works from anywhere. If you are already in /home/jesus, the relative path scripts reaches the same folder, and .. goes up one level. ABSOLUTE vs RELATIVE PATHS / (root) etc home var jesus ~ scripts ABSOLUTE/home/jesus/scriptsstarts at /, works from anywhere RELATIVEfrom ~ : scriptsdepends on where you are . this directory · .. one level up · ~ your home directory

An absolute path starts at the root; a relative one, at the directory you are in. Diagram: The House of Routing.

cd                 # to your home directory (same as cd ~)
cd /etc            # absolute path
cd ..              # goes up one level
cd -               # returns to the previous directory
cd scripts         # relative path from where you are

Listing with ls

OptionWhat it does
ls -lLong format: permissions, owner, size and date
ls -aIncludes hidden files (those starting with a dot, such as .bashrc)
ls -lhHuman-readable sizes: K, M, G
ls -SSorts by size, largest first
ls -t / ls -rSorts by date / reverses the order
ls -RRecursive listing of subdirectories
ls -dShows the directory itself, not its contents

Create, copy, move and delete

touch notes.txt              # creates an empty file (or updates its timestamp)
mkdir lab                    # creates a directory
mkdir -p lab/docker/v1       # creates the whole path, even if it does not exist
mkdir "my photos"            # names with spaces: quotes...
mkdir my\ photos2            # ...or a backslash

cp notes.txt lab/            # copies a file
cp -r lab lab-copy          # copies an entire directory
mv notes.txt study-notes.txt     # renames
mv study-notes.txt lab/          # moves

rm lab/study-notes.txt           # deletes a file
rmdir lab-empty              # deletes an empty directory
rm -r lab-copy              # deletes a directory and everything in it
There is no trash can in the Linux terminal. What rm deletes is not easy to recover. Use rm -i so it asks you for confirmation, and double-check any rm -r.

A useful difference: cp creates a new file, owned by whoever copies it; mv within the same file system only changes the name or location and keeps the owner and permissions.

Wildcards (globbing) and history

Wildcards let you refer to many files at once. The shell expands them before running the command:

WildcardMeaningExample
*Any string, even an empty onels *.log
?Exactly one characterls report-?.txt
[abc]One of the characters in the listls file[123].txt
[a-z]One character in the rangels [a-c]*
[!abc]Any character except thosels [!0-9]*
[[:digit:]]Classes: digits, letters (alpha), uppercase (upper)…ls *[[:digit:]].txt

A tip from your notes: build the pattern starting from the end of the name, which is usually the most distinctive part (the extension), and work your way to the left.

History

history          # lists the commands you ran
!123             # repeats command number 123
!!               # repeats the last one (handy: sudo !!)
Ctrl+R           # searches backward through history
set +o history   # stops recording commands in this session; set -o history turns it back on

Linux Essentials-style challenge

Answer before you open the solution. Getting it wrong here is the cheapest way to learn.

1. What does echo '$HOME' print?

The literal text $HOME. Single quotes prevent variable expansion; with double quotes you would see the path to your home directory.

2. You create a script in ~/scripts and want to run it by name in all your future sessions. What do you do?

Add export PATH="$PATH:$HOME/scripts" to ~/.bashrc and reload it with source. The script must have execute permission.

3. You are in /home/ana. Is docs/report.txt an absolute or relative path, and which file does it point to?

Relative, because it does not start with /. It points to /home/ana/docs/report.txt.

4. Which files does ls ?.txt show?

The .txt files whose name has exactly one character, such as a.txt or 1.txt, but not ab.txt.

What you take away

  • Command, options and arguments; and Linux is case-sensitive.
  • Double quotes expand variables; single quotes do not.
  • export passes variables to programs; PATH decides where commands are looked up and becomes persistent in ~/.bashrc.
  • Help: --help for the quick version, man for the full one; find files with find.
  • Absolute paths start with /; relative ones, from where you are. ., .. and ~ are your shortcuts.
  • touch, mkdir -p, cp -r, mv, rm -r: carefully, because there is no trash can.
  • *, ? and [ ] select many files at once.

In the next chapter you discover the power of the command line: archiving and compressing, searching inside files, pipes and your first scripts.

Official resources

Source and credits. Chapter written from Jesús Espinoza's study notes for the LPI Linux Essentials certification (2022), updated to version 2.0 of the exam (010-200). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details were checked against the official documentation from LPI, GNU and the distributions.

Navigate