0%

The Command Line, Enough of It

Moving Around

So far the shell has stayed in one place. cd moves it.

cd data
pwd
/workspace/data

The shell is now standing inside data. Run ls and you will see books.txt and nothing else, not because the rest of the project is gone, but because you are looking at a different directory.

Going back up

There is a name for “the directory above this one”, and it is two dots:

cd ..
pwd
/workspace

.. is not a special command, it is a name you can use in any path, the same way you use a directory’s real name. cd .. means go up one. ls .. means the directory above. cd ../.. means go up two.

Relative and absolute

Every path you have written so far has been relative: it starts from wherever you happen to be. data/books.txt means “from here, go into data, then take books.txt”. Move somewhere else and the same text points at something else, or at nothing at all.

An absolute path starts from the top of the filesystem and is therefore the same from everywhere. On this machine the top is /, so the to the reading list is:

cat /workspace/data/books.txt

You can type that from anywhere and get the same file. The giveaway is the leading /.

Your own machine works the same way as the terminal here. Absolute paths start at /, and your files live somewhere under /home/ and your username.

Run cd / and then ls on your own machine to see the top of the filesystem this course’s paths are measured from.

Both kinds are useful and neither is more correct. are short and they survive being moved somewhere else as a group. Absolute paths are unambiguous and they work no matter where the shell happens to be. When a command cannot find a file, an absolute path is the fastest way to find out whether the file is missing or you are simply somewhere else.

Computer Fundamentals walks the same ground with more examples, including the ~ shortcut for your home directory, and drive letters on Windows:

Why this is the thing that trips people up

Almost every “it works for you but not for me” problem a beginner hits with the terminal is a working directory problem. The file is there. The command is right. The shell is standing somewhere else.

pwd and ls, in that order, answer it nearly every time.

You are in /workspace and cat data/books.txt works. You then run cd data. What happens if you run cat data/books.txt again?

Next: making something of your own, from the terminal.