Exercise: Pack for the Trip

Get a trip folder ready to go by finding what takes the space with du and df, giving a deep folder a short name with ln -s, packing archives with tar and zip, and opening one a friend sent, while a check script tells you which parts are done.

Apprentice · Unix · Linux · Updated

You leave for Lisbon next week, and the trip folder is a mess: photos from three days out, a drone video, two playlists, the bookings buried four folders deep, and an archive your friend Maya sent with her tips for the city. Before you go, you'll find out what takes up the space, give the tickets folder a short name, pack the documents and the photos to send, and look inside Maya's archive before you open it. A check script looks at the trip folder and your answers and tells you which parts are done.

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, and two of the steps need zip and unzip. A Mac has zip and unzip already; Ubuntu often doesn't have zip until you install it. If bats --version, zip --version or unzip -v doesn't print a version number yet, install what's missing:

sudo apt install bats zip unzip    # 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-pack-for-the-trip, 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:

  • trip/: the trip's photos, video, music, documents, packing list and from-maya.tar.gz. This is where you work.
  • answers.txt: the eight steps. Four have a numbered line for your answer; the other four are things to do in the trip folder.
  • 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 trip. Each step in answers.txt looks like one of these, with the example already answered:

# Example: which file in the trip folder is the packing list?
# 0) packing-list.txt

# 1. Which folder in trip takes up the most space? (Write the folder's name only.)
1)

# 4. Make a link named tickets in the trip folder that points to documents/bookings/2026/lisbon/tickets.
#    (Nothing to write here.)
  1. Measure. Steps 1 to 3 are about space: which folder is biggest, which folder inside photos is biggest, and which column of df -h shows the room that's left.
  2. Link. Step 4: a link named tickets, right in the trip folder.
  3. Pack. Steps 5 and 6: documents.tar.gz and photos.zip, both saved in the trip folder.
  4. Open Maya's archive. Step 7: list it and find the file in its maps folder, without unpacking it. Step 8: then unpack it in the trip folder.
  5. Answer. Open answers.txt in any text editor (or in nano), write the answers to steps 1, 2, 3 and 7 after their ), and save.

Check your work

From the exercise folder (not from inside trip):

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 Task 6: photos.zip doesn't hold photos/beach/sandcastle.jpg. Without -r, zip packs only the folder's name, not what's in it. Check with unzip -l photos.zip.

Hints

  • Biggest first: du -sh * | sort -hr, as in What's Filling My Disk? du and df. The folder with the most files isn't always the biggest.
  • The link's order: the real folder first, then the link's name, the same order as cp. ls -l shows where a link points (Shortcuts to Files: ln -s).
  • A link gone wrong: remove it with rm tickets, no / at the end, and make it again. Running ln -s a second time doesn't replace it.
  • Read tar's letters aloud: create, zip, file; the f comes last, right before the archive's name (Many Files as One: tar and zip).
  • List before you unpack: tar -tzf shows what's inside, and names ending in / are folders.

When you're done

Every check passes:

check.bats
 ✓ the trip folder is still here
 ✓ 1) the biggest folder in trip
 ✓ 2) the biggest folder in photos
 ✓ 3) the df column for the room that's left
 ✓ 4) a link named tickets
 ✓ 5) the documents folder packed into documents.tar.gz
 ✓ 6) the photos folder packed into photos.zip
 ✓ 7) the file in the maps folder of Maya's archive
 ✓ 8) Maya's archive unpacked in the trip folder

9 tests, 0 failures

Now look back at step 4. What would happen to your tickets link if you renamed the lisbon folder to portugal, and what are the two ways to fix it?

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-pack-for-the-trip), 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:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-bash-pack-for-the-trip.git
    git push -u origin main
    

    If 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-pack-for-the-trip.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-pack-for-the-trip, 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

Finding what fills a disk, giving a deep folder a short name and packing a folder to send are jobs you'll do on your own computer, on servers and before every trip.

Key terms

Related