# Exercise: Sort the Downloads Folder

Sort a messy Downloads folder of photos, songs, a lease, a resume and a year of receipts into folders with mkdir, mv and cp, wildcards and braces, without replacing a thing, and let a check script tell you what's done.

Apprentice · Unix · Linux · Updated November 10, 2026

Everyone has a Downloads folder like this one: phone photos, a couple of screenshots, songs, a lease, a resume, school papers and a whole year of receipts, all in one pile of 30 files. Your job is to sort it into folders, a few commands at a time, without losing or replacing anything on the way. There's one trap in the pile: a file that has the same name as one already sorted. A check script looks at the folder when you're done and tells you which parts 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**, the program that ran your earlier exercise checks. 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-sort-the-downloads-folder](https://github.com/ask-codey/exercise-bash-sort-the-downloads-folder), 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:

- `downloads/`: the pile to sort. Every file is a small stand-in with one line of text, not a real photo or PDF. `downloads/documents/` already holds last year's `lease.pdf`.
- `check.bats`: the check. You don't need to change it.
- `README.md`: the job, in short.

## What to do

Open a terminal, `cd` into the exercise folder (the one with `check.bats` in it), then `cd downloads` and look around with `ls`. Then sort the pile:

1. **Make the folders.** `photos` and `music` in `downloads`, and `receipts` with four folders inside it, `q1` to `q4`, one for each quarter of the year.
2. **The bus pass.** The screenshot from October 2, `Screenshot 2026-10-02 at 09.14.55.png`, is your bus pass. Put it in `documents`, named `bus-pass.png`.
3. **Photos.** Every other `.jpg` and `.png` goes in `photos`.
4. **Music.** Every `.mp3` goes in `music`.
5. **Receipts.** Each receipt goes in its quarter's folder: January to March in `q1`, April to June in `q2`, July to September in `q3`, and October to December in `q4`. The month is the number after `2026-` in the name, as in `receipt-2026-04-22.pdf`.
6. **The new lease.** `documents` already has last year's `lease.pdf`. Keep it, and put the new `lease.pdf` beside it, named `lease-2026.pdf`.
7. **Your resume.** You're about to edit it, so keep a copy first: `resume.docx.bak`, beside `resume.docx`. Both go in `documents`.
8. **The rest.** The other `.pdf` files go in `documents`. When you're done, nothing is left loose in `downloads`: only folders.

Use the terminal for all of it, and don't edit, empty or delete any file.

## Check your work

From the exercise folder (not from inside `downloads`):

```bash
bats check.bats
```

At first almost every check fails. That's expected: each part you finish turns its check into a ✓. A failing check says what's still out of place and gives a hint, for example *These songs aren't in music yet: downloads/podcast-episode-12.mp3 downloads/rainy-day-mix.mp3 downloads/song-for-the-road.mp3. One mv with \*.mp3 moves them all.*

## Hints

- **Echo first.** Before any `mv` with a wildcard, `echo` the same pattern and read the list (*Wildcards: \*, ? and [ ]*). If the list is wrong, the move would be wrong too.
- **Order matters.** The receipts, the bus pass and the new lease are files a broad pattern such as `*.pdf` or `*.png` would also catch. Sort the special ones first.
- **One folder per quarter, one line.** `mkdir -p` makes a folder and the folders around it, and braces name all four quarters at once (*Brace Expansion: Twelve Folders in One Line*).
- **A quarter is three months.** `[ ]` matches one character from a range, so a single pattern can pick out three months' receipts.
- **Nothing gets replaced.** `mv -i` asks before it replaces a file, and `mv -n` never does (*Copy, Move and Rename: cp and mv*). If last year's lease does get replaced, there's no undo: download the exercise again and start over.
- **Moved something to the wrong folder?** Move it again: `mv` works from any folder to any other. An extra folder made by mistake does no harm, because the check only looks at the ones it names.

## When you're done

Every check passes:

```text
check.bats
 ✓ the downloads folder is still here
 ✓ 1) photos, music and documents are folders in downloads
 ✓ 2) receipts has a folder for each quarter, q1 to q4
 ✓ 3) all nine photos are in photos
 ✓ 4) the bus pass is documents/bus-pass.png
 ✓ 5) all three songs are in music
 ✓ 6) every receipt is in its quarter's folder
 ✓ 7) last year's lease is still documents/lease.pdf
 ✓ 8) the new lease is documents/lease-2026.pdf
 ✓ 9) resume.docx and its backup, resume.docx.bak, are in documents
 ✓ 10) the other documents are in documents
 ✓ 11) nothing is left loose in downloads

12 tests, 0 failures
```

Now count your commands. How many did the whole pile take, and how many would it have taken with one `mv` for each of its 30 files?

## 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-sort-the-downloads-folder`), 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-sort-the-downloads-folder.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-sort-the-downloads-folder.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-sort-the-downloads-folder`, 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

Sorting a folder of 30 files, or 3,000, is the same handful of commands: make the folders, check a pattern with `echo`, move. It's the everyday cleanup that IT technicians, photographers and anyone with a full Downloads folder do, in minutes instead of an afternoon.

## Key terms

- [mkdir](https://www.ask-codey.com/glossary/mkdir)
- [mv](https://www.ask-codey.com/glossary/mv)
- [cp](https://www.ask-codey.com/glossary/cp)
- [Wildcard](https://www.ask-codey.com/glossary/wildcard)
- [Brace Expansion](https://www.ask-codey.com/glossary/brace-expansion)

## Related

- [Making Folders and Files: mkdir and touch](https://www.ask-codey.com/lessons/bash-making-folders-and-files)
- [Copy, Move and Rename: cp and mv](https://www.ask-codey.com/lessons/bash-copy-move-rename)
- [Wildcards: \*, ? and \[ \]](https://www.ask-codey.com/lessons/bash-wildcards)
- [Brace Expansion: Twelve Folders in One Line](https://www.ask-codey.com/lessons/bash-brace-expansion)

Source: https://www.ask-codey.com/lessons/exercise-bash-sort-the-downloads-folder
