Exercise: The Archive Detective
Solve a small mystery in the town archive by searching two weeks of door logs with grep and a regular expression, tracking down a locker, a hidden file and a big scan with find, and clearing out leftover files with find -exec, while a check script tells you which answers are right.
Apprentice · Unix · Linux · Updated
The town archive's brass telescope has vanished from its display case in the reading room. The archivist has two weeks of door logs, a staff list, a basement full of lockers and the shelves of the stacks, and no time to read any of it. You'll search the logs for a visit nobody should have made, follow the badge to a name and the name to a locker, look for what's hidden there, and tidy up after the archive's new scanner on the way. A check script compares your answers with the archive itself and tells you which ones are right.
Before you start
You need a terminal: the Terminal app on a Mac, or Ubuntu on Linux or in WSL on Windows. The check runs with bats. If you've done an earlier exercise, you already have it. If bats --version doesn't print a version number yet, install it:
sudo apt install bats # Ubuntu, and Ubuntu in WSL
brew install bats-core # a Mac, with Homebrew
sudo asks for your password, the one you use to sign in; nothing shows on the screen while you type it.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-bash-the-archive-detective, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder. Or clone it with Git.
The folder holds:
archive/: the archivist'snotice.txt,staff.txt, alogsfolder with one file a day, and thelockers,stacksandreading-roomfolders. This is where you search.answers.txt: the eight steps. Seven have a numbered line for your answer; the last is a job to do in the archive.check.bats: the check. You don't need to change it.
What to do
Open a terminal, cd into the exercise folder (the one with answers.txt in it), then cd archive and read notice.txt. Each step in answers.txt looks like this, with the example already answered:
# Example: which file in the archive folder lists who carries which badge?
# 0) staff.txt
# 1. One night, a reading-room door opened between midnight and 6 in the morning. Which log
# file is that in? (Write the file's name, such as 2026-11-01.log.)
1)
- The night visit. Steps 1 and 2: find the one reading-room line with a time between
00:00and05:59, in any day's log, and its badge number. Some visits at 8 in the morning start with0too, so describe the time with a pattern. - The name. Step 3: whose badge is it? Step 4: how many lines in all the logs mention the telescope? The guards weren't careful with capitals.
- The locker. Steps 5 and 6: find that person's locker, and the hidden file inside it.
- The stacks. Step 7: one file there is over 1 MB. Step 8: delete every
.tmpfile in the archive, and nothing else. Look at the list withechobefore you delete anything. - Answer. Open
answers.txtin any text editor (or in nano), write the answers to steps 1 to 7 after their), and save.
Check your work
From the exercise folder (not from inside archive):
bats check.bats
At first almost every check fails. That's expected: each step you finish turns its check into a ✓. A failing check names its step and gives a hint, for example Question 4: "1" isn't the number of lines yet. The guards wrote telescope in different capitals: grep needs -i, and -r to search the whole logs folder.
Hints
- A whole folder:
grep -rsearches every file in a folder, and-lshows only the names of the files that match (Finding Text: grep). - Times as a pattern:
^ties a pattern to the start of the line, and[0-5]is any one digit from 0 to 5 (Patterns: Regular Expressions for grep). Build the pattern a piece at a time, and check each piece. - Capitals: grep is case-sensitive until you add
-i. - Names and sizes:
findtakes a starting folder, then tests such as-name "*part*",-type dor-size +1M. Quote the name pattern (Finding Files: find). - Hidden things have names that start with a dot.
ls -ashows them, and so doesfindwith-name ".*". - Deleting with find: run the line with
echoin front ofrmfirst, and delete only when the list is right.
When you're done
Every check passes:
check.bats
✓ the archive folder is still here
✓ 1) the log file with the night visit
✓ 2) the badge that opened the reading room
✓ 3) the name on that badge
✓ 4) the lines that mention the telescope
✓ 5) the path to the locker
✓ 6) the hidden file in the locker
✓ 7) the file in stacks that's over 1 MB
✓ 8) every .tmp file deleted, and nothing else
9 tests, 0 failures
Now look back at step 1. Which pieces of your pattern did the real work, and what would grep -r '^0' have found instead?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-bash-the-archive-detective), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (Ubuntu in WSL on Windows), send your work to your new repo. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-bash-the-archive-detective.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-bash-the-archive-detective.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-bash-the-archive-detective, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
Searching a pile of logs for one line, following it to a name and a folder, and cleaning up exactly the files you meant to: that's a working day for anyone who looks after a server, a website or a busy shared drive.