Exercise: The Moving-Day List

Finish a new apartment's moving-day jobs in the terminal, deleting the trash safely, removing an empty box, writing a door sign and a to-do list, fixing a setting in nano and reading the ends of long files, then let a check script tell you what's done.

Apprentice · Unix · Linux · Updated

Sam and Maya have moved into a new apartment, and the hallway is still full of boxes, bubble wrap and a vase that didn't make it. Somewhere in that mess is a birth certificate that must not end up in the trash. Your job is to work through the moving-day list in the terminal: clear the hallway safely, unpack one box, write a sign and a to-do list, fix the thermostat, and read the ends of two long files. A check script looks at the apartment and tells you which jobs 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, a small program that runs checks written for bash. 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-moving-day-list, 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:

  • apartment/: the new apartment. This is where you work.
  • answers.txt: three questions, each with a numbered line for your answer.
  • 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 apartment and look around with ls. Then work through the list:

  1. Throw out the trash. In hallway, delete every bubble-wrap file and broken-vase.txt, and keep birth-certificate.txt. Look before you delete, as in Deleting Safely: rm and rmdir.
  2. Unpack the book box. Move the three books from hallway/box-books into living-room with mv (Copy, Move and Rename: cp and mv), then remove the empty box-books folder. Leave hallway/box-bedroom as it is: it isn't unpacked yet.
  3. Make a door sign. Write door-sign.txt in the apartment folder, holding one line of words of your own.
  4. Add to the to-do list. Add two lines to the end of todo.txt, keeping the three it has: - buy curtains and - call the internet company.
  5. Fix the thermostat. Open thermostat.conf in nano and change temperature=78 to temperature=70. Change nothing else.
  6. Read the ends of two long files. Answer the three questions in answers.txt with head and tail. Open answers.txt in nano (nano ../answers.txt from inside apartment), write each answer after its ), and save. The file starts like this:
# Example: what is the first word of apartment/todo.txt?
# 0) TO

# 1. How many boxes did the movers bring? The header in the first lines of apartment/packing-list.txt says.
1)

Check your work

From the exercise folder (not from inside apartment):

bats check.bats

At first most checks fail. That's expected: each job you finish turns its check into a ✓. A failing check names the job and gives a hint, for example thermostat.conf still says temperature=78. Open it with nano, change 78 to 70, save with Ctrl+O and Enter, then quit with Ctrl+X.

Hints

  • A pattern can match more than you think. Run ls with the exact pattern first, or use rm -i and answer n for anything you want to keep.
  • rmdir refuses a folder with things in it. That's the point: move the books out first. Don't reach for rm -r on a box that isn't empty.
  • One arrow replaces, two arrows add. If todo.txt loses its lines, put them back from inside apartment with printf 'TO DO\n- unpack\n- find the remote\n' > todo.txt, then add the new lines with >>.
  • nano's keys are listed at the bottom of its screen: ^O is Ctrl+O, which saves, and ^X is Ctrl+X, which quits.
  • The answers are at the ends of the files. head -n 5 shows the first five lines and tail -n 1 the last one. A log's newest line is at the bottom.

When you're done

Every check passes:

check.bats
 ✓ the apartment folder is still here
 ✓ the bubble wrap and the broken vase are gone
 ✓ the birth certificate is still in the hallway
 ✓ the books are in the living room
 ✓ the empty book box is gone
 ✓ the bedroom box still holds the photo album
 ✓ door-sign.txt holds one line
 ✓ todo.txt kept its three lines and gained the two new ones
 ✓ the thermostat is set to 70
 ✓ 1) how many boxes the movers brought
 ✓ 2) the box with the spare key
 ✓ 3) when the last box came upstairs

12 tests, 0 failures

Now look back at the to-do list. You added two lines with >>. How would you add ten at once, without typing >> todo.txt ten times? (Writing to Files: echo, > and >> shows one way.)

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-moving-day-list), 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-the-moving-day-list.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-the-moving-day-list.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-moving-day-list, 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

Deleting without regret, writing files from a single line, fixing a setting with no mouse and reading the end of a log are the everyday jobs of anyone who looks after a computer, from a laptop to a website's server.

Key terms

Related