StackMonsters
Switch to light mode
Blog

Linux Command Line Practice Exercises for Beginners (With Answers)

· 9 min read · StackMonsters

Reading about the command line doesn't make you faster at it. Typing does. These exercises cover the habits beginners need first: passing arguments, quoting, flags, reading error messages, finding help, and moving around the filesystem. Each one has a task, the answer, and an explanation of why it works.

You can do them in any Linux terminal, in WSL on Windows, or on macOS. The answers show GNU coreutils output, which is what Ubuntu, Debian and Fedora ship. macOS uses BSD versions of some commands, so a few flags and messages differ there. If you don't have a terminal handy, each section ends with a free browser lab that covers the same skill, with a Linux machine that checks your answers.

Set up the practice files

Paste this block into your terminal. It creates ~/practice with a few small files and touches nothing else:

bash
mkdir -p ~/practice/shop/staff ~/practice/shop/reports ~/practice/shop/orders
cd ~/practice
printf 'web-01\nweb-02\ndb-01\ncache-01\n' > servers.txt
printf 'risk: low\nrollback: redeploy v2.3.9\n' > 'release notes.txt'
printf '09:00 deploy started\n09:01 pulled image v2.4.0\n09:02 ERROR health check failed on web-02\n09:03 rolled back to v2.3.9\n09:04 deploy finished\n' > deploy.log
printf 'cake 4\ncoffee 3\ntea 2\n' > shop/prices.txt
printf 'mon ana\ntue ben\nwed ana\nthu chris\nfri ben\n' > shop/staff/rota.txt
printf 'order 1041 cake\norder 1042 tea\n' > shop/orders/monday.txt
printf 'order 1043 coffee\norder 1044 cake\norder 1045 cake\n' > shop/orders/tuesday.txt
printf 'best day: friday\ntop item: cake\n' > shop/reports/week-41.txt
printf 'best day: monday\ntop item: coffee\n' > shop/reports/week-40.txt
printf 'TILL_PIN=0000\n' > shop/.till
touch -t 202610060900 shop/reports/week-40.txt
touch -t 202610130900 shop/reports/week-41.txt

When you're done, rm -r ~/practice removes all of it.

1. Arguments: what a command is given

Everything after the command name, separated by spaces, is an argument. The command decides what an argument means. cat treats each one as a file to open; echo just prints them.

Exercise 1.1. Print servers.txt and deploy.log with a single command. What is the last line of the output?

Show the answer
bash
cat servers.txt deploy.log

The last line is 09:04 deploy finished. cat opens its arguments in order and writes them one after the other, which is where its name comes from: concatenate.

Exercise 1.2. What does echo servers.txt print, and why isn't it the list of servers?

Show the answer

It prints servers.txt. echo never opens files; it writes its arguments back out. The shell doesn't know that servers.txt is a file name, so it hands the same text to whichever command you run. What happens next is up to that command.

2. Quotes: keeping spaces inside one argument

The shell splits a line into arguments at spaces before the command ever runs. A file name that contains a space is therefore two arguments unless you quote it.

Exercise 2.1. Run cat release notes.txt. How many errors do you get, and what file names do they mention?

Show the answer

Two:

console
$ cat release notes.txt
cat: release: No such file or directory
cat: notes.txt: No such file or directory

cat received two arguments, release and notes.txt, and tried to open each one. Neither exists.

Exercise 2.2. Print the file correctly. Which risk level does it name?

Show the answer

Quote the whole name, or escape the space:

bash
cat 'release notes.txt'
cat "release notes.txt"
cat release\ notes.txt

All three pass one argument, and the file says risk: low. Pressing Tab after typing cat rel also works: completion inserts the backslash for you.

Exercise 2.3. Make echo print status: and ready with four spaces between them.

Show the answer
console
$ echo status:    ready
status: ready
$ echo "status:    ready"
status:    ready

Without quotes, the shell splits at the run of spaces and throws the spaces away, so echo gets two arguments and joins them with one space. With quotes, echo gets one argument that still contains the four spaces.

Free lab · Linux Essentials · 4 minKeep the SpacesRuns in your browser. No install, no account.

3. Flags: changing what a command does

A flag (or option) is an argument that starts with - and changes how the command behaves. Short flags are one letter (-l); long flags are words (--lines). Some flags take a value: in head -n 2, the 2 belongs to -n.

Exercise 3.1. How many lines and how many words are in deploy.log? Use one flag for each.

Show the answer
console
$ wc -l deploy.log
5 deploy.log
$ wc -w deploy.log
22 deploy.log

Plain wc deploy.log prints lines, words and bytes together. -l counts newline characters, so a file whose last line doesn't end in a newline reports one line fewer than you'd expect. Files written by printf '...\n' and most editors end with one.

Exercise 3.2. On which line number of deploy.log is the word ERROR?

Show the answer
console
$ cat -n deploy.log
     1	09:00 deploy started
     2	09:01 pulled image v2.4.0
     3	09:02 ERROR health check failed on web-02
     4	09:03 rolled back to v2.3.9
     5	09:04 deploy finished

Line 3. -n numbers every output line. Once you know grep, grep -n ERROR deploy.log prints only the matching line with its number.

Exercise 3.3. Print the first two lines of deploy.log, then only the last line.

Show the answer
bash
head -n 2 deploy.log
tail -n 1 deploy.log

Without -n, both head and tail print 10 lines. tail is the one you'll use most: tail -n 50 app.log reads the end of a log, and tail -f app.log keeps printing new lines as they're written.

Free lab · Linux Essentials · 5 minChange How a Command WorksRuns in your browser. No install, no account.

4. Reading error messages

Most Unix error messages have the same shape: who complained, then what it was working on, then why it failed. Read them left to right and they tell you which part of the line to fix.

Exercise 4.1. Run each line and say which program wrote the error and what you'd change:

bash
wcc -l servers.txt
wc -z servers.txt
cat server.txt
Show the answer
console
$ wcc -l servers.txt
bash: wcc: command not found
$ wc -z servers.txt
wc: invalid option -- 'z'
Try 'wc --help' for more information.
$ cat server.txt
cat: server.txt: No such file or directory
  • bash: wcc: command not found comes from the shell, not from wc. No program named wcc exists, so nothing ran. Fix the command name.
  • wc: invalid option -- 'z' comes from wc. It started, read its arguments, and rejected a flag it doesn't have. Fix the flag. The wording varies a little between versions; some print unknown option -- z.
  • cat: server.txt: No such file or directory comes from cat, about the argument server.txt. The file is called servers.txt.

The first word of the message is the fastest way to know where to look: a shell error means the line never reached the program.

Free lab · Linux Essentials · 5 minFix Three Broken LinesRuns in your browser. No install, no account.

5. Finding help without searching the web

Two sources ship with the system. --help prints a short summary of the flags; it's handled by the program itself, so most GNU tools have it. man opens the full manual page; press / to search inside it and q to quit.

Exercise 5.1. Using only wc --help, find the flag that counts characters, and use it on servers.txt.

Show the answer
console
$ wc --help | grep -- --chars
  -m, --chars            print the character counts
$ wc -m servers.txt
29 servers.txt

-c counts bytes, which is the same number for plain ASCII text and larger once a file has accented letters or emoji. -m counts characters.

Exercise 5.2. How many lines does tail print when you don't give -n? Find it in the help text, not by counting.

Show the answer

Ten. The first line of tail --help says "Print the last 10 lines of each FILE to standard output." On macOS, tail has no --help; man tail documents the same default.

Free lab · Linux Essentials · 5 minFind the FlagRuns in your browser. No install, no account.

6. Moving around the filesystem

Every shell has a current directory. A path that starts with / is absolute and works from anywhere; any other path is relative and starts from the current directory. .. means the parent directory and ~ means your home directory.

Exercise 6.1. From ~/practice, go into shop/staff and print the full path of where you are. Who works on Friday?

Show the answer
console
$ cd shop/staff
$ pwd
/home/you/practice/shop/staff
$ cat rota.txt
mon ana
tue ben
wed ana
thu chris
fri ben

Ben. Once you're in staff, rota.txt needs no path, because a bare file name is a relative path from the current directory.

Exercise 6.2. Without leaving staff, print the shop's prices with a relative path.

Show the answer
bash
cat ../prices.txt

.. is the parent of the current directory, shop. You can chain it: ../../servers.txt is two levels up, in practice.

Exercise 6.3. Move to shop/reports in one cd, and find which report changed most recently.

Show the answer
console
$ cd ../reports
$ ls -lt
total 8
-rw-r--r-- 1 you you 32 Oct 13 09:00 week-41.txt
-rw-r--r-- 1 you you 34 Oct  6 09:00 week-40.txt

week-41.txt. -l shows the long listing with dates, and -t sorts by modification time, newest first. Add -r to reverse it so the newest is at the bottom, next to your prompt.

Exercise 6.4. Go up to shop. ls shows four entries. How many does ls -a show, and what is the extra file?

Show the answer
console
$ cd ..
$ ls
orders  prices.txt  reports  staff
$ ls -a
.  ..  .till  orders  prices.txt  reports  staff

.till. A name that starts with a dot is hidden from a plain ls. That's the only thing that makes it hidden; there is no hidden attribute. . and .. are always there and refer to the directory itself and its parent. Configuration files like ~/.bashrc and ~/.ssh are hidden the same way.

Exercise 6.5. Go back to the directory you were in before, then go home, using the shortest commands you can.

Show the answer
bash
cd -     # back to the previous directory, and print it
cd       # with no argument: your home directory

cd ~ also goes home, but plain cd is shorter.

Free lab · Linux Essentials · 5 minTake the Short WayRuns in your browser. No install, no account.

7. History: stop retyping commands

Exercise 7.1. Run history, pick the number next to an earlier cat command, and run it again by number without retyping it.

Show the answer

If cat servers.txt deploy.log is number 14, type !14. Bash prints the expanded command and then its output:

console
$ !14
cat servers.txt deploy.log
web-01
...

The first line is bash showing you what it's about to run. It isn't output from cat. !! reruns the previous line, which is useful after sudo !! when you forgot sudo. For anything further back, Ctrl+R searches your history as you type, and is usually faster than reading numbers.

Free lab · Linux Essentials · 4 minReplay Your CommandsRuns in your browser. No install, no account.

Where to go next

If these were easy, the next skills are searching text with grep, chaining commands with pipes (|), and redirecting output to files (> and >>). Then come permissions and your first shell script. Each one builds on what you practised here: arguments, quoting, flags and paths show up in every command you'll run.