# 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 November 10, 2026

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:

```bash
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](https://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's `notice.txt`, `staff.txt`, a `logs` folder with one file a day, and the `lockers`, `stacks` and `reading-room` folders. 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:

```text
# 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)
```

1. **The night visit.** Steps 1 and 2: find the one reading-room line with a time between `00:00` and `05:59`, in any day's log, and its badge number. Some visits at 8 in the morning start with `0` too, so describe the time with a pattern.
2. **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.
3. **The locker.** Steps 5 and 6: find that person's locker, and the hidden file inside it.
4. **The stacks.** Step 7: one file there is over 1 MB. Step 8: delete every `.tmp` file in the archive, and nothing else. Look at the list with `echo` before you delete anything.
5. **Answer.** Open `answers.txt` in 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`):

```bash
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 -r` searches every file in a folder, and `-l` shows 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:** `find` takes a starting folder, then tests such as `-name "*part*"`, `-type d` or `-size +1M`. Quote the name pattern (*Finding Files: find*).
- **Hidden things** have names that start with a dot. `ls -a` shows them, and so does `find` with `-name ".*"`.
- **Deleting with find:** run the line with `echo` in front of `rm` first, and delete only when the list is right.

## When you're done

Every check passes:

```text
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.

1. 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.
2. In a terminal in your exercise folder (Ubuntu in WSL on Windows), send your work to your new repo. If you downloaded the ZIP:

   ```bash
   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 main
   ```

   If you cloned the starter instead, point your copy at your own repo, then commit and push:

   ```bash
   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 main
   ```

3. Paste 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.

## Key terms

- [grep](https://www.ask-codey.com/glossary/grep)
- [Regular Expression in the Shell](https://www.ask-codey.com/glossary/regular-expression-in-the-shell)
- [find](https://www.ask-codey.com/glossary/find-unix)
- [Hidden File](https://www.ask-codey.com/glossary/hidden-file)

## Related

- [Finding Text: grep](https://www.ask-codey.com/lessons/bash-grep)
- [Patterns: Regular Expressions for grep](https://www.ask-codey.com/lessons/bash-regular-expressions)
- [Finding Files: find](https://www.ask-codey.com/lessons/bash-find)

Source: https://www.ask-codey.com/lessons/exercise-bash-the-archive-detective
