Exercise: Find Your Way

Explore a summer camp made of folders with pwd, ls, cd and Tab, write down what you find, and let a check script tell you which answers are right.

Novice · Unix · Linux · Updated

Pine Lake Summer Camp keeps everything in folders: the cabins, the dining hall, the boathouse, even its lost and found. A camper has lost a water bottle, someone has a secret snack stash, and the pantry is hiding the campfire marshmallows. Your job is to explore the camp in the terminal and answer eight questions about it. A check script compares your answers with the camp 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, 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-find-your-way, 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:

  • camp/: the camp's folders and files. Explore them; don't change them.
  • answers.txt: the eight questions, each with a numbered line for your answer. This is the file you write.
  • check.bats: the check. You don't need to change it.

What to do

Open a terminal and cd into the exercise folder, the one with answers.txt in it. Then cd camp and start exploring. Each question in answers.txt looks like this, with the example already answered:

# Example: what is the name of the file in camp that welcomes you?
# 0) welcome.txt

# 1. The boathouse has a hidden folder. What is the folder's name?
1)
  1. Explore. Move around the camp with cd, look with ls, ls -a and ls -l, and check where you are with pwd whenever you lose track.
  2. Answer. Open answers.txt in any text editor, write each answer after its ), and save. Write names exactly as ls shows them, dots included.
  3. The last two questions want a relative path for one cd command. Try your path in the terminal first: if cd gets you there, it's right.

Check your work

From the exercise folder (not from inside camp):

bats check.bats

At first almost every check fails. That's expected: each answer you get right turns its check into a ✓. A failing check names its question and gives a hint, for example Question 8: from cabin-2, cd "../boathouse" doesn't reach the boathouse. Each .. goes up one folder: how many do you need to get back to camp?

Hints

  • Lost? pwd tells you where you are, and cd on its own takes you home, so you can start again from there.
  • Hidden things have names that start with a dot. Plain ls skips them; ls -a shows them (see Looking Around: ls).
  • Sizes are in ls -l, as the number just before the date. Folders don't count as files.
  • Spaces in a name: put the name in quotes, or type its first letters and let Tab finish it (Let the Shell Type for You: Tab).
  • Counting .. steps: say each folder out loud on the way up, as in Moving Around: cd, .. and ~.

When you're done

Every check passes:

check.bats
 ✓ the camp folder is still here
 ✓ 1) the boathouse's hidden folder
 ✓ 2) the file inside the hidden folder
 ✓ 3) how many things are in cabin-2
 ✓ 4) the biggest file in the dining hall
 ✓ 5) the cabin with a hidden folder
 ✓ 6) the file in arts and crafts
 ✓ 7) a relative path from camp to the pantry
 ✓ 8) a relative path from cabin-2 to the boathouse

9 tests, 0 failures

Now look back at question 8. Could you get from cabin-2 to the boathouse with an absolute path instead? Which would you rather type, and which still works if someone moves the whole camp folder somewhere else?

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-find-your-way), 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-find-your-way.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-find-your-way.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-find-your-way, 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

Exploring a folder you've never seen, finding what's hidden in it and describing how to get from one place to another is exactly what you'll do on every new project, server and computer you work on.

Key terms

Related