Linux — Common Commands: Practice Exercises

7 minute read

These hands-on exercises cover navigation, file management, viewing file contents, getting help, and command history. Work through them in order — each builds on the last.


Exercise 1 — Navigate Like a Pro

Difficulty: ⭐ Easy

  1. Open a terminal and run pwd — note your starting directory
  2. Navigate to /var/log using an absolute path
  3. List all files with their sizes in human-readable format, sorted by size (largest first)
  4. Navigate to /etc/ using a relative path (hint: cd ..)
  5. Use pushd to save your location and jump to /tmp
  6. Use popd to return to /etc/
  7. Navigate home using the shortest command possible
  8. Use tree -L 2 /etc and identify two directories you don’t recognise — look them up with man or ls
💡 Show Answer
# 1. Starting directory
pwd
# /home/ubuntu

# 2. Navigate to /var/log (absolute path)
cd /var/log
pwd
# /var/log

# 3. List files by size
ls -lhS
# total 64M
# -rw-r----- 1 syslog adm   45M Apr  1 10:00 syslog
# -rw-r----- 1 syslog adm   12M Apr  1 10:00 auth.log
# drwxr-xr-x 2 root   root 4.0K Apr  1 09:00 apt
# ...

# 4. Navigate to /etc (relative — go up one, then into etc)
cd ../etc
pwd
# /etc

# 5. pushd to /tmp (saves /etc)
pushd /tmp
# /tmp /etc

# 6. popd back to /etc
popd
# /etc
pwd
# /etc

# 7. Navigate home (shortest: just 'cd' with no arguments)
cd
pwd
# /home/ubuntu

# 8. Tree of /etc
tree -L 2 /etc | head -40
# Two interesting directories: /etc/cron.d/ (cron jobs), /etc/skel/ (new user template)
man 5 crontab   # Learn about /etc/cron.d format
ls /etc/skel/   # Files placed in new user home directories

Exercise 2 — File Management Sandbox

Difficulty: ⭐⭐ Medium

Create a complete project directory structure and practise file operations:

  1. Create this directory structure in /tmp:
    /tmp/myproject/
    ├── src/
    ├── tests/
    ├── docs/
    └── scripts/
    
  2. Create README.md, Makefile, and .gitignore in /tmp/myproject/
  3. Create main.py in src/ and test_main.py in tests/
  4. Copy README.md to docs/README-copy.md
  5. Rename Makefile to Makefile.bak
  6. Create a symlink: /tmp/myproject-link → /tmp/myproject
  7. Use tree to verify the final structure
  8. Clean up by deleting /tmp/myproject and /tmp/myproject-link
💡 Show Answer
# 1. Create the directory structure
mkdir -p /tmp/myproject/{src,tests,docs,scripts}

# 2. Create files in the project root
touch /tmp/myproject/{README.md,Makefile,.gitignore}

# 3. Create source files
touch /tmp/myproject/src/main.py
touch /tmp/myproject/tests/test_main.py

# 4. Copy README.md to docs/
cp /tmp/myproject/README.md /tmp/myproject/docs/README-copy.md

# 5. Rename Makefile
mv /tmp/myproject/Makefile /tmp/myproject/Makefile.bak

# 6. Create a symlink
ln -s /tmp/myproject /tmp/myproject-link
ls -la /tmp/myproject-link
# lrwxrwxrwx 1 ubuntu ubuntu 14 Apr  1 10:00 /tmp/myproject-link -> /tmp/myproject

# 7. Verify with tree
tree /tmp/myproject
# /tmp/myproject
# ├── .gitignore
# ├── Makefile.bak
# ├── README.md
# ├── docs
# │   └── README-copy.md
# ├── scripts
# ├── src
# │   └── main.py
# └── tests
#     └── test_main.py

# 8. Clean up
rm -rf /tmp/myproject
rm /tmp/myproject-link

Exercise 3 — Log File Analysis

Difficulty: ⭐⭐ Medium

Analyse the system log file:

  1. Count the total number of lines in /var/log/syslog (or /var/log/messages on RHEL/CentOS)
  2. Show the first 5 lines
  3. Show the last 10 lines
  4. Search for the word “error” (case-insensitive) and count how many lines contain it
  5. Find the 5 most recent unique process names in the log (column 5 typically)
  6. Use tail -f to monitor the log for 30 seconds — open a second terminal and run sudo systemctl restart cron — observe new log entries
💡 Show Answer
# 1. Count lines
wc -l /var/log/syslog
# 12456 /var/log/syslog

# 2. First 5 lines
head -5 /var/log/syslog
# Apr  1 00:00:00 server-01 systemd[1]: Starting Daily apt download activities...
# Apr  1 00:00:00 server-01 systemd[1]: Started Daily apt download activities.
# ...

# 3. Last 10 lines
tail -10 /var/log/syslog

# 4. Count lines with "error" (case-insensitive)
grep -ic "error" /var/log/syslog
# 42

# 5. Most frequent process names (field 5: "processname[pid]:")
awk '{print $5}' /var/log/syslog | \
    sed 's/\[.*\]//' | \
    sed 's/://' | \
    sort | \
    uniq -c | \
    sort -rn | \
    head -5
# 1234 systemd
#  892 kernel
#  456 cron
#  234 sshd
#   89 sudo

# 6. Follow the log in terminal 1:
tail -f /var/log/syslog

# In terminal 2:
sudo systemctl restart cron

# Back in terminal 1, you should see:
# Apr  1 10:05:00 server-01 systemd[1]: Stopping cron daemon...
# Apr  1 10:05:00 server-01 systemd[1]: Starting cron daemon...
# Apr  1 10:05:00 server-01 systemd[1]: Started cron daemon.

Exercise 4 — Getting Help Challenge

Difficulty: ⭐⭐ Medium

Use only built-in help tools (no Google, no Stack Overflow) to answer these questions:

  1. What flag makes cp prompt before overwriting files?
  2. What does sort -u do? (Use man sort)
  3. What section of the man pages covers file formats?
  4. Use apropos to find a command that displays disk usage
  5. Use tldr to find the syntax for extracting a .tar.gz file
  6. What does Ctrl+R do in bash? (Search the bash man page: man bash, then search /reverse)
💡 Show Answer
# 1. cp flag for prompting before overwrite
cp --help | grep -i "interac"
# -i, --interactive          prompt before overwrite
# Answer: -i

# 2. sort -u
man sort
# /unique
# -u, --unique     with -c, check for strict ordering; without -c, output only the first
#                  of an equal run
# Answer: sort and output only unique lines (equivalent to sort | uniq but faster)

# 3. Man page sections for file formats
man man
# /Sections
# 5   File formats and conventions, e.g. /etc/passwd

# 4. apropos for disk usage
apropos "disk usage"
# du (1)             - estimate file space usage
# df (1)             - report file system disk space usage

# 5. tldr for tar.gz extraction
tldr tar
# - E[x]tract a (compressed) archive [f]ile into the current directory:
#   tar xf source.tar[.gz|.bz2|.xz]
# Answer: tar xf archive.tar.gz

# 6. Find reverse-i-search in bash man page
man bash
# /reverse-i-search
# reverse-i-search (C-r)
#   Search backward starting at the current line and moving `up' through the
#   history as necessary. This is an incremental search.
# Answer: Ctrl+R starts a reverse incremental search through command history

Exercise 5 — Command History Mastery

Difficulty: ⭐⭐⭐ Challenging

This exercise tests your history and shortcuts:

  1. Run these commands (in order):
    echo "line 1" > /tmp/testfile.txt
    echo "line 2" >> /tmp/testfile.txt
    echo "line 3" >> /tmp/testfile.txt
    cat /tmp/testfile.txt
    
  2. Without retyping it, re-run the cat command using !!
  3. Use !echo to run the last echo command again — what happens?
  4. Add these aliases to your shell session (not permanently):
    • cf → cat /tmp/testfile.txt
    • af → echo "new line" >> /tmp/testfile.txt
  5. Run cf to show the file, then af three times, then cf again
  6. Search your history with Ctrl+R for the echo "line 1" command
  7. Set HISTCONTROL=ignoredups and run ls five times — then check history to confirm only one ls was saved
💡 Show Answer
# 1. Set up the test file
echo "line 1" > /tmp/testfile.txt
echo "line 2" >> /tmp/testfile.txt
echo "line 3" >> /tmp/testfile.txt
cat /tmp/testfile.txt
# line 1
# line 2
# line 3

# 2. Re-run cat with !!
!!
# cat /tmp/testfile.txt (executed)
# line 1
# line 2
# line 3

# 3. Use !echo — re-runs the LAST echo command
!echo
# echo "line 3" >> /tmp/testfile.txt   ← adds "line 3" again

# 4. Add aliases
alias cf='cat /tmp/testfile.txt'
alias af='echo "new line" >> /tmp/testfile.txt'

# 5. Use the aliases
cf
# line 1
# line 2
# line 3
# line 3  (added by !echo in step 3)

af
af
af

cf
# line 1
# line 2
# line 3
# line 3
# new line
# new line
# new line

# 6. Ctrl+R search
# Press Ctrl+R, type "line 1"
# (reverse-i-search)`line 1': echo "line 1" > /tmp/testfile.txt
# Press Ctrl+C or Ctrl+G to cancel without running

# 7. ignoredups test
export HISTCONTROL=ignoredups
ls
ls
ls
ls
ls
history | tail -10
# ... 
# 100  export HISTCONTROL=ignoredups
# 101  ls               ← only ONE ls entry, not five

Chapter Summary

After completing these exercises you should be able to:

  • ✅ Navigate the filesystem using absolute and relative paths, pushd/popd
  • ✅ Create complex directory structures with mkdir -p
  • ✅ Copy, move, rename, and delete files safely
  • ✅ Analyse log files using head, tail, wc, grep, and awk
  • ✅ Use man, --help, apropos, and tldr to learn any command
  • ✅ Recall and reuse commands with !!, !string, and Ctrl+R

← Back to Command History and Shortcuts Next Chapter → Linux File System

Updated: