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:
- Throw out the trash. In
hallway, delete everybubble-wrapfile andbroken-vase.txt, and keepbirth-certificate.txt. Look before you delete, as in Deleting Safely: rm and rmdir. - Unpack the book box. Move the three books from
hallway/box-booksintoliving-roomwithmv(Copy, Move and Rename: cp and mv), then remove the emptybox-booksfolder. Leavehallway/box-bedroomas it is: it isn't unpacked yet. - Make a door sign. Write
door-sign.txtin theapartmentfolder, holding one line of words of your own. - Add to the to-do list. Add two lines to the end of
todo.txt, keeping the three it has:- buy curtainsand- call the internet company. - Fix the thermostat. Open
thermostat.confin nano and changetemperature=78totemperature=70. Change nothing else. - Read the ends of two long files. Answer the three questions in
answers.txtwithheadandtail. Openanswers.txtin nano (nano ../answers.txtfrom insideapartment), 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
lswith the exact pattern first, or userm -iand answernfor anything you want to keep. rmdirrefuses a folder with things in it. That's the point: move the books out first. Don't reach forrm -ron a box that isn't empty.- One arrow replaces, two arrows add. If
todo.txtloses its lines, put them back from insideapartmentwithprintf '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:
^Ois Ctrl+O, which saves, and^Xis Ctrl+X, which quits. - The answers are at the ends of the files.
head -n 5shows the first five lines andtail -n 1the 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.
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.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 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-moving-day-list.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-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.