0%

The Command Line · practice

Navigate a Project Tree

When a command cannot find a file, the file is usually right where it always was. You are standing somewhere else.

There are three questions worth asking, in this order: where am I, what is here, and where should I go? Every answers all three, and only the spelling changes.

If you arrived from another course

The beside this lesson holds a disposable project on a separate computer. Its starting directory is /workspace; these are not the files on your own machine. Use that hosted terminal for the graded practice below.

Before starting, you need to report the current directory and resolve a from it. If either is unfamiliar, use these two lessons, then return here. The first explains how to open the terminal and check your location; the second explains /, ., and .. through a worked directory tree.

What is here

You already know how to ask where you are. Listing what surrounds you is the next question:

ls

A fresh chapter workspace holds this:

/workspace
├── README.md
├── checkpoint/
├── data/
│   ├── README.txt
│   └── measurements.txt
├── destination/
├── environment-check.py
├── notes with spaces.txt
└── report.py

Where should I go

Changing directories takes one command, and the habit worth building is to check afterward rather than assume:

cd data
pwd

That destination is a relative path, so it resolved from where you already were. Everything you worked out on paper in the previous chapter applies here unchanged: two dots name the parent, a leading slash starts at the filesystem root, and a tilde stands for your home directory. The shell is simply the first place those rules do something visible.

Climbing back out uses the parent:

cd ..
pwd

An means the same thing from anywhere. Here that is /workspace; on your own machine it might be C:\Users\Sam\project or /home/sam/project. Never copy a sample username. Ask where you are and use what the shell tells you.

Leave one piece of evidence

Everything so far only looked. The graded check cannot see where your shell travelled earlier, so you will leave one directory at the destination as evidence that your relative path resolved correctly. The creation command is supplied here; working out the route is the lesson.

mkdir example-directory

mkdir is short for “make directory”. It normally prints nothing when it works, so list the parent as a separate check.

Creating the directory changes the tree, so use Reset before the practice if you ran the example. Then list the parent after your real command and confirm that the marker landed where you intended.

Practice: leave a marker through a relative path

In the previous chapter you resolved relative paths on paper: write down where you are, take each segment in turn, go up for .., and check where you landed. This is where that stops being an exercise on paper. The shell is standing somewhere specific, and a path you write from the wrong starting point will land somewhere real and wrong.

This one is graded from the workspace. Use Reset first if an earlier attempt left destination/navigation-complete behind.

  1. Confirm you are at /workspace.

  2. List the directory and check that data and destination are both there.

  3. Move into data and confirm you arrived.

  4. From inside data, and without using an absolute path, create a directory called navigation-complete inside the sibling folder destination. You will need .. to climb out of data first.

  5. Return to /workspace and list destination to confirm navigation-complete is there.

Step 4 is the whole lesson in one command. You have to know where you are standing before you can write a relative path that lands somewhere else, and the section above showed the command on a throwaway example. Working out the path from data to its sibling is yours.

Leave every supplied file where it is and stay inside /workspace. The check requires the marker directory and verifies key supplied files; use Reset first so unrelated leftovers do not muddy your own listing. Then press Check my work.

You are in /workspace/data and move to ../checkpoint. Where do you end up?

You create a directory, and the command prints nothing. What is the next thing to do?

You can find where you are, see what surrounds you, and move deliberately. Before changing anything, though, you need to know what kind of thing you are pointing at.