Module 6 · Command Line 35 min

The terminal is the black window where you type commands. It looks scary at first. It isn't. Every command in this module does something you already do on Windows — once you see which Windows thing each one matches, it stops being strange.

By the end of this module, you will:

  • Execute at least 15 essential terminal commands from memory
  • Navigate the filesystem, create, copy, move, and delete files via the terminal
  • Use command flags and arguments to modify command behaviour
  • Combine commands using pipes to filter and process output
  • Use wildcards (*, ?, [abc], {a,b,c}) to operate on multiple files at once
  • Chain commands with ;, &&, || and recall past commands with !!, !$, history
  • Inspect files efficiently with head, tail, and less instead of cat
bash — your new home
$ pwd
/home/user
$ ls -la
drwxr-xr-x Desktop Documents Downloads
$ cd Documents
$ mkdir my-project
$ ls
my-project
$

The Windows command, the Linux command — side by side

ls -la
Windows: dir
Show every file in this folder — even the hidden ones — with their size, date and permissions.
click to copy
cd ~
Windows: cd %USERPROFILE%
Jump to your home folder. ~ is shorthand for home. cd - sends you back to the folder you were just in.
click to copy
pwd
Windows: cd (no args)
"Where am I?" — prints the full path of the folder you're standing in. Stands for "print working directory".
click to copy
mkdir -p
Windows: mkdir
Make a new folder. The -p bit lets you make folders inside folders in one go: mkdir -p a/b/c.
click to copy
cp -r
Windows: copy / xcopy
Copy a file. For a whole folder, add -r. Example: cp file.txt backup.txt makes a copy called backup.txt.
click to copy
mv
Windows: move / rename
Move a file, OR rename it — same command does both. mv old.txt new.txt renames old.txt to new.txt.
click to copy
rm -rf
Windows: del / rmdir
Delete — no Recycle Bin, no undo. -r means "and everything inside the folder". -f means "don't ask, just do it". Read what comes after this twice before pressing Enter.
click to copy
cat
Windows: type
Show what's inside a file, right there in the terminal. cat file1 file2 shows both, one after the other.
click to copy
grep -r
Windows: findstr
Look inside files for a word. grep -r "error" /var/log/ hunts for "error" in every log file at once.
click to copy
find
Windows: dir /s /b
Find files by their name, date or size. find ~ -name "*.pdf" finds every PDF anywhere in your home folder.
click to copy
man
Windows: ??? (nothing this good)
The built-in instruction book. man ls shows you everything ls can do. Press q to close it.
click to copy
sudo
Windows: Run as Admin
Run the next command as the admin user. Stick sudo on the front any time a command needs admin power.
click to copy

Pipes and redirection — connecting commands together

pipes & redirection
# Pipe | — send output of one command into another
$ ls -la | grep ".txt"
-rw-r--r-- 1 user user 1234 notes.txt

# Redirect > — save output to a file
$ ls -la > filelist.txt

# Append >> — add to end of a file
$ echo "new line" >> notes.txt

# Count matching lines
$ grep -r "error" /var/log/ | wc -l
47

The most dangerous command on Linux

sudo rm -rf / wipes everything on your computer — instantly. No warning. No undo. No Recycle Bin. Linux trusts you to know what you're doing. The rule: never copy a command off the internet and run it until you understand every part of it. If you're not sure, don't run it.

The dollar sign — don't type it

All through this course you'll see commands written like this: $ ls -la. The $ at the start is not part of the command. It's the little symbol your terminal shows to say "I'm ready, type something." Type only what comes after the $. Every new Linux person gets caught by this once. Now you won't.

Try it now Type this — and don't type the dollar sign: echo "Hello Linux"
You should see: Hello Linux

Two keys that save you most of the typing

These two shortcuts barely get mentioned in Linux books, but every experienced Linux user hits them all day. Learn them now and the rest of this course gets a lot easier.

Tab — let Linux finish the typing for you

TabPress once and Linux fills in the rest of the word for you. Press twice and it shows every option.
↑ ArrowBring back the last command you ran. Press again to go further back in your history.
Ctrl+RSearch what you've typed before. Type any part of an old command and Linux finds it for you.
Ctrl+CStop whatever's running right now. If a command seems frozen, this gets you out.
Ctrl+LWipe the screen clean. Same thing as typing clear, just faster.

Try it: type cd /ho then hit Tab. Linux finishes it for you as cd /home/. Type the first letter of your username, hit Tab again, and you're at your home folder — without typing the whole path.

Wildcards — handle lots of files in one go

A wildcard is a special character that means "match a bunch of files at once". Linux swaps the wildcard for the matching filenames before the command runs. So rm *.tmp deletes every file in this folder ending in .tmp. Useful — and a little dangerous, because once it runs there's no undo. Always run ls with the same wildcard first to check what would be hit.

Four wildcards you'll use every week

*Match anything, any length. ls *.pdf shows every PDF. rm log_* deletes every file whose name starts with "log_".
?Match exactly one character. ls report-?.txt matches report-1.txt and report-A.txt, but not report-10.txt (that's two characters).
[abc]Match any one of the listed characters. ls file[123].log matches file1.log, file2.log and file3.log.
{a,b,c}Run the same command for each item in the list. cp report.{txt,bak} is shorthand for "copy report.txt to report.bak".

Try it: ls *.html in this course folder lists every HTML file. Now picture pairing that with grep, cp, or — carefully — rm.

Chaining commands, and history shortcuts

A pipe (|) sends the output of one command straight into the next one. Three more symbols join commands by result — "do this, then if it worked do that". And the history shortcuts pull back old commands without you typing them again.

Joining commands together

cmd1 ; cmd2Do cmd1, then do cmd2. Doesn't care whether cmd1 worked.
cmd1 && cmd2Do cmd2 only if cmd1 worked. The classic: mkdir build && cd build — make the folder, then move into it (but only if the folder got made).
cmd1 || cmd2Do cmd2 only if cmd1 failed. Handy for a "plan B".
cmd &Run the command in the background — the terminal hands you back the prompt straight away. Bring it back with fg.

Bringing back what you typed before

!!Run the last command again. Forgot to put sudo on the front? Type sudo !! and you're done.
!$The last thing from the previous command. After mkdir docs, type cd !$ and you move into docs.
!aptRun the last command you typed that started with "apt". Saves a lot of up-arrow presses.
historyList every command you've ever run. Pipe it into grep to find one: history | grep ssh.

Reading big files — head, tail, less (not cat)

cat dumps the whole file onto your screen all at once. Fine for tiny files. Awful for a log file with 50,000 lines. These three commands are how experienced Linux people actually read files.

Command What it does Useful extras
head fileShows the first 10 lines (handy for a CSV header).head -20 file shows the first 20 lines.
tail fileShows the last 10 lines (handy for recent log entries).tail -f file keeps watching the file and prints new lines as they appear. Brilliant for watching logs as something happens.
less fileLets you scroll through a huge file one page at a time.Same keys as the man pages: Space to scroll, /word to search, q to quit, G to jump to the bottom.

A real-world trick: open two terminals. Run tail -f /var/log/syslog in one. Do whatever you're debugging in the other. The system tells you what it's doing, live, as you do it.

Nano — a 30-second intro to a text editor

Some of the commands in this module save things to files (the >> ones). Soon you'll want to edit one of those files. Nano is the friendliest Linux text editor — it shows its shortcuts right at the bottom of the screen, so you never have to remember them. Four shortcuts is all you need to function:

The four nano commands you need right now

nano fileOpen a file for editing. If the file doesn't exist yet, nano makes a new one.
Ctrl+OSave (stands for "write out"). Press Enter to confirm the filename.
Ctrl+XClose nano. If you've made changes you haven't saved, it asks before quitting.
Ctrl+WSearch the file. Type the word you want to find and press Enter.

Module 13 covers nano and vim properly. This is enough to start.

The built-in instruction book — man pages

Every Linux command comes with its own instruction book. Type man ls to read the manual for the ls command. The manual opens in a special reader — the screen doesn't look like the normal terminal any more. Here's how to move around it:

Moving around a man page

qQuit. Closes the manual and drops you back at the prompt.
SpaceScroll down one page.
/keywordSearch. Type / then a word, press Enter. Press n to jump to the next match.
GSkip to the bottom. The EXAMPLES section is almost always down there — and it's usually the most useful part.

Tip: when a man page feels like too much, type /EXAMPLE and jump straight to real working examples. Working commands are always easier to copy than long explanations.

Squashing files into one file — tar, gzip, zip

Sooner or later you'll want to bundle a whole folder into a single file — to email a project, back up a folder, or open something somebody else has packed up. On Windows you right-click and pick "Send to → Compressed (zipped) folder". On Linux it's a one-line command. Two words to know: archive (one file that contains lots of files) and compression (squashing that file to make it smaller). On Linux you usually do both in one go.

What you want to do Command What's going on
Pack a folder into one squashed file (the everyday one)tar -czf project.tar.gz project/c = create, z = squash with gzip, f = "the filename is next". .tar.gz is the standard Linux pack-and-squash format.
Open a .tar.gz back outtar -xzf project.tar.gzx = extract. The files land in the folder you're standing in.
Peek inside without unpacking ittar -tzf project.tar.gzt = list. Shows everything in the archive without unpacking.
Squash one single filegzip notes.txtReplaces notes.txt with notes.txt.gz.
Unsquash a .gz filegunzip notes.txt.gz or gzip -d notes.txt.gzThe opposite of gzip.
Make a .zip (so a Windows person can open it)zip -r project.zip project/Use this when you're sending the file to someone on Windows — Windows opens .zip on its own. For .tar.gz they'd need 7-Zip.
Open a .zipunzip project.zipWorks the same on every Linux.

Two other squashers you might bump into: bzip2 (older, slower, squashes a tiny bit smaller — file ends in .tar.bz2, swap the z flag for j) and xz (newer, slowest, smallest of all — file ends in .tar.xz, flag is J). For everyday work gzip is good enough. Only reach for the others when somebody hands you a file in that format.

Try it now Pack and squash a folder, then check the size before and after. Watch it shrink:
du -sh ~/Documents · tar -czf docs-backup.tar.gz ~/Documents · du -sh docs-backup.tar.gz

Why Linux uses .tar.gz, not .zip

Back in the day, tar (short for "tape archive") just packed files together, and gzip just squashed them. Two simple tools, each doing one job. Sticking them together gives you .tar.gz. The good thing about this format: it remembers Unix file permissions and symlinks. .zip often loses those. So Linux software ships as .tar.gz, and .zip is reserved for sharing files with Windows users.