Linux — Common Commands: Practice Exercises
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
- Open a terminal and run
pwd— note your starting directory - Navigate to
/var/logusing an absolute path - List all files with their sizes in human-readable format, sorted by size (largest first)
- Navigate to
/etc/using a relative path (hint:cd ..) - Use
pushdto save your location and jump to/tmp - Use
popdto return to/etc/ - Navigate home using the shortest command possible
- Use
tree -L 2 /etcand identify two directories you don’t recognise — look them up withmanorls
💡 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:
- Create this directory structure in
/tmp:/tmp/myproject/ ├── src/ ├── tests/ ├── docs/ └── scripts/ - Create
README.md,Makefile, and.gitignorein/tmp/myproject/ - Create
main.pyinsrc/andtest_main.pyintests/ - Copy
README.mdtodocs/README-copy.md - Rename
MakefiletoMakefile.bak - Create a symlink:
/tmp/myproject-link→/tmp/myproject - Use
treeto verify the final structure - Clean up by deleting
/tmp/myprojectand/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:
- Count the total number of lines in
/var/log/syslog(or/var/log/messageson RHEL/CentOS) - Show the first 5 lines
- Show the last 10 lines
- Search for the word “error” (case-insensitive) and count how many lines contain it
- Find the 5 most recent unique process names in the log (column 5 typically)
- Use
tail -fto monitor the log for 30 seconds — open a second terminal and runsudo 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:
- What flag makes
cpprompt before overwriting files? - What does
sort -udo? (Useman sort) - What section of the man pages covers file formats?
- Use
aproposto find a command that displays disk usage - Use
tldrto find the syntax for extracting a.tar.gzfile - What does
Ctrl+Rdo 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:
- 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 - Without retyping it, re-run the
catcommand using!! - Use
!echoto run the lastechocommand again — what happens? - Add these aliases to your shell session (not permanently):
cf→cat /tmp/testfile.txtaf→echo "new line" >> /tmp/testfile.txt
- Run
cfto show the file, thenafthree times, thencfagain - Search your history with
Ctrl+Rfor theecho "line 1"command - Set
HISTCONTROL=ignoredupsand runlsfive times — then check history to confirm only onelswas 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, andawk - ✅ Use
man,--help,apropos, andtldrto learn any command - ✅ Recall and reuse commands with
!!,!string, andCtrl+R
| ← Back to Command History and Shortcuts | Next Chapter → Linux File System |