Terminal Exercises
Time to practise across all the shells. Each exercise names its shell and has a starter playground that already runs and prints a placeholder, so you can see the format. Replace the TODO parts, press ▶ Run and compare with the "Expected" lines. The solutions are runnable too, but try for a few minutes first: that's where the learning happens.
Every playground starts in an empty folder, so each one creates the files it needs.
| # | Exercise | Shell | Topics |
|---|---|---|---|
| 1 | Project skeleton | bash | mkdir -p, touch, find |
| 2 | The same skeleton in CMD | CMD | mkdir, echo >, dir /b /o:n |
| 3 | Files per extension | PowerShell | Get-ChildItem, Group-Object |
| 4 | Back up only if it exists | bash | &&, ||, cp |
| 5 | A greeting with a default | CMD | call :label, %~1, if |
| 6 | Count the errors | PowerShell | Select-String, pipeline |
| 7 | Orders per customer | bash | cut, sort, uniq -c, awk |
| 8 | Arguments and quoting | zsh | script file, shebang, chmod +x, $@ |
| 9 | Fix Windows line endings | bash | CRLF, grep -c, sed |
| 10 | Batch rename | CMD | ren with wildcards |
1. Project skeleton (bash)
Create this structure with as few commands as possible: a folder app containing src, tests and docs, an empty app/src/main.py, and app/README.md containing the line # app. Then print every path, sorted.
Expected:
app
app/README.md
app/docs
app/src
app/src/main.py
app/tests# TODO: create the folders and files
mkdir app
find app | sortapp
Show solution
mkdir -p app/src app/tests app/docs
touch app/src/main.py
echo "# app" > app/README.md
find app | sortapp
app/README.md
app/docs
app/src
app/src/main.py
app/tests
mkdir -p creates parent folders as needed and accepts several paths at once. In bash you can shorten the first line further with brace expansion: mkdir -p app/{src,tests,docs}.
2. The same skeleton in CMD
Build the same structure in a batch file and list the contents of app and app\src in name order.
Expected:
docs
README.md
src
tests
---
main.py@echo off
rem TODO: create the folders and files
mkdir app
dir /b /o:n app
echo ------
Show solution
@echo off
mkdir app\src app\tests app\docs
type nul > app\src\main.py
echo # app> app\README.md
dir /b /o:n app
echo ---
dir /b /o:n app\srcdocs
README.md
src
tests
---
main.py
CMD's mkdir creates missing parent folders automatically (like mkdir -p), and type nul > file is the batch way to create an empty file. Note that the sort is case-insensitive: docs comes before README.md.
3. Files per extension (PowerShell)
Count the files in reports by extension and print one line per extension, alphabetically, in the form .ext: count.
Expected:
.csv: 3
.md: 1
.txt: 2New-Item -ItemType Directory reports | Out-Null
'jan.csv', 'feb.csv', 'mar.csv', 'notes.txt', 'todo.txt', 'README.md' |
ForEach-Object { New-Item -Path reports -Name $_ | Out-Null }
# TODO: group the files by extension
$files = Get-ChildItem reports -File
"$($files.Count) files"6 files
Show solution
New-Item -ItemType Directory reports | Out-Null
'jan.csv', 'feb.csv', 'mar.csv', 'notes.txt', 'todo.txt', 'README.md' |
ForEach-Object { New-Item -Path reports -Name $_ | Out-Null }
Get-ChildItem reports -File |
Group-Object Extension |
Sort-Object Name |
ForEach-Object { "$($_.Name): $($_.Count)" }.csv: 3
.md: 1
.txt: 2
Group-Object makes one group per distinct value of the property; each group has a Name and a Count. The bash version of this task is ls reports | sed 's/.*\./. /' | sort | uniq -c.
4. Back up only if it exists (bash)
Write a function backup that copies a file into backup/ only if the file exists, printing backed up NAME on success and skipped NAME otherwise. Use && and || instead of if. Then list the backup folder.
Expected:
backed up app.cfg
skipped db.cfg
backed up users.cfg
app.cfg
users.cfgecho "port=8080" > app.cfg
echo "admin=ada" > users.cfg
mkdir -p backup
backup() {
echo "TODO $1"
}
backup app.cfg
backup db.cfg
backup users.cfg
ls backupTODO app.cfg
TODO db.cfg
TODO users.cfg
Show solution
echo "port=8080" > app.cfg
echo "admin=ada" > users.cfg
mkdir -p backup
backup() {
[ -f "$1" ] && cp "$1" backup/ && echo "backed up $1" || echo "skipped $1"
}
backup app.cfg
backup db.cfg
backup users.cfg
ls backupbacked up app.cfg
skipped db.cfg
backed up users.cfg
app.cfg
users.cfg
The chain stops at the first failing command: if [ -f ... ] fails, cp and the first echo are skipped and || runs. In CMD the same idea is if exist "%~1" (copy "%~1" backup\ >nul && echo backed up %~1) else echo skipped %~1.
5. A greeting with a default (CMD)
Write a subroutine :greet that prints Hello, NAME!, using World when no name is passed. Call it three times: with no argument, with Ada, and with "Grace Hopper" (in quotes, since it contains a space).
Expected:
Hello, World!
Hello, Ada!
Hello, Grace Hopper!@echo off
call :greet
call :greet Ada
call :greet "Grace Hopper"
exit /b
:greet
rem TODO: use %~1, with World as the default
echo Hello, %1!
exit /bHello, !
Hello, Ada!
Hello, "Grace Hopper"!
Show solution
@echo off
call :greet
call :greet Ada
call :greet "Grace Hopper"
exit /b
:greet
set "name=%~1"
if not defined name set "name=World"
echo Hello, %name%!
exit /bHello, World!
Hello, Ada!
Hello, Grace Hopper!
%~1 removes the surrounding quotes; %1 keeps them. set "name=%~1" with an empty argument removes the variable, so if not defined detects it. The first exit /b stops the main part from falling through into the subroutine.
6. Count the errors (PowerShell)
From app.log, print how many lines contain ERROR, then each distinct error message (the text after ERROR ) once, sorted.
Expected:
4 errors
disk full
timeout
user not found@'
10:01 INFO start
10:02 ERROR timeout
10:03 WARN slow
10:04 ERROR disk full
10:05 INFO retry
10:06 ERROR timeout
10:07 ERROR user not found
'@ | Set-Content app.log
# TODO: find the ERROR lines and their messages
"? errors"? errors
Show solution
@'
10:01 INFO start
10:02 ERROR timeout
10:03 WARN slow
10:04 ERROR disk full
10:05 INFO retry
10:06 ERROR timeout
10:07 ERROR user not found
'@ | Set-Content app.log
$errors = Select-String -Path app.log -Pattern 'ERROR (.+)