Moving around the filesystem

Beginner · 6 min

Understand the working directory, absolute and relative paths, ., .., ~ and cd -, then use pushd and popd to hop between directories without losing your place.

Before thisGetting help with man, help and type

Microsoft Developer · 6 min

Moving around the filesystem

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Microsoft DeveloperReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The filesystem is a tree rooted at /. Every shell is always standing in one directory, its working directory, shown by pwd.
  • An absolute path starts with / and means the same thing everywhere. A relative path is resolved starting from the working directory.
  • . is the current directory, .. is its parent, ~ is your home, and cd - returns to the previous directory.
  • A directory name with spaces must be quoted for cd, or the shell passes it two words and cd fails with 'too many arguments'.
  • pushd and popd keep a stack of directories so you can jump away and come back. pwd -P shows the real location when a symlink is involved.

The shell is always standing somewhere

A file manager shows you a folder. A shell has no window of folders, so it keeps one fact in memory: the working directory, the place where relative names get looked up. Open a terminal and you are standing in one. pwd (“print working directory”) says which:

$ pwd
/SCR/lab4

I’m writing /SCR where my real scratch path was, to keep the examples short. The whole filesystem is a tree. It has one root, written /, and every file and directory hangs somewhere below it. There are no drive letters. A path like /SCR/lab4/site reads as: start at the root, go into SCR, then lab4, then site.

Absolute and relative paths

A path that starts with / is absolute. It names the same place no matter where you are. A path that doesn’t start with / is relative: the shell starts from the working directory. Think of an address: “12 Main Street, Springfield” works from anywhere, while “second door on the left” only works from the doorway you’re standing in.

Four special names make relative paths short:

  • . the current directory
  • .. its parent
  • ~ your home directory, where a fresh shell starts
  • - (only with cd) the previous directory

My test tree was site/src/components, site/docs, and a folder called my notes. Here is a session:

$ cd site/src
$ echo "now: $PWD   before: $OLDPWD"
now: /SCR/lab4/site/src   before: /SCR/lab4
$ cd ../docs
$ pwd
/SCR/lab4/site/docs
$ cd -
/SCR/lab4/site/src
$ cd ..; pwd
/SCR/lab4/site

Every cd stores the old location in $OLDPWD and the new one in $PWD. cd - swaps them, and prints where it landed so you don’t lose track. cd with no argument goes home, which I confirmed was my scratch home, and cd / goes to the root.

. matters more than it looks: ./run.sh means the file run.sh in this directory, which is how you run a program that isn’t on your PATH, as the next lesson explains.

When it goes wrong

Two errors you will see often. A name with a space gets split into two words by the shell, so cd is given two arguments. A name that doesn’t exist fails with a status of 1, which a script can check:

$ cd my notes
bash: cd: too many arguments
$ echo $?
2
$ cd "my notes"; pwd
/SCR/lab4/my notes
$ cd nowhere
bash: cd: nowhere: No such file or directory
$ echo $?
1

(My script printed t4.sh: line 14: before each message. Typed interactively you see bash:.) Quoting the name keeps it as one word, as you saw in the anatomy lesson.

The directory stack

Going back and forth between two places, cd - is enough. With three or more, use a stack. pushd changes directory and remembers where you were. popd goes back. dirs -v shows the stack:

$ pushd site/src >/dev/null; pushd ../docs; dirs -v
/SCR/lab4/site/docs /SCR/lab4/site/src /SCR/lab4
 0  /SCR/lab4/site/docs
 1  /SCR/lab4/site/src
 2  /SCR/lab4
$ popd; popd; pwd
/SCR/lab4/site/src /SCR/lab4
/SCR/lab4
/SCR/lab4

The video’s pushd and popd section is built around exactly this “stack” idea, which it introduces at 2:25.

A directory can be a symbolic link, a pointer to another directory (the links lesson covers them). The shell remembers the path you typed, not where it ended up, so pwd can differ from the real location:

$ ln -s site/src shortcut; cd shortcut; pwd; pwd -L; pwd -P
/SCR/lab4/shortcut
/SCR/lab4/shortcut
/SCR/lab4/site/src
$ cd ..; pwd
/SCR/lab4

pwd -L (the default) shows the logical path, pwd -P the physical one. Note that cd .. went back to lab4, where I came from, not to site, the real parent of src. Bash follows the path you typed. If you want the real parent, cd -P .. does that.

CDPATH, briefly

CDPATH lists directories where cd also looks for a relative name. I tried it:

$ cd /; CDPATH=/private/tmp cd claude-501
/private/tmp/claude-501

It worked from /, where claude-501 doesn’t exist, and cd printed the directory it chose. Most people never set this, and it can confuse scripts. I mention it because a surprise cd that prints a path is usually CDPATH.

About the video

Microsoft Developer’s episode 5 of 20 in its Bash for Beginners series (the title is about getting around the terminal with Bash) was published 15 February 2023 and runs 6 minutes 7 seconds. Its description lists pwd at 0:00, cd at 1:28, “What is a stack?” at 2:25 and pushd/popd at 3:35. English by title and description. I couldn’t read a transcript, so I don’t know whether it mentions ./.., cd -, or symlinks; those parts of this lesson are my own. It’s a series episode, so it may assume the earlier episodes. Learn Linux TV’s longer 2019 video on the same topic is the runner-up.

What I did not run

I never ran these on Linux. cd -P, mentioned above, I did not run either, so treat that line as documented behaviour. Home was a scratch directory, not my real one.

Next

You can move through a tree. Typing a bare command name moves through a different list, the one the shell searches to find programs: the PATH and how commands are found.

Check yourself

  1. 01You are in /srv/site/src. What does cd ../docs do?

    It moves to /srv/site/docs. .. means the parent directory, so the relative path goes up one level and then down into docs.

  2. 02How is cd - different from cd ..?

    cd .. goes to the parent directory. cd - goes to wherever you were before the last cd, which is stored in $OLDPWD, and it prints that directory.

  3. 03cd my notes fails with 'too many arguments'. Why?

    Unquoted, the space splits the name into two words and cd accepts only one. Write cd "my notes" or cd my\ notes.

  4. 04You cd into a symlink. pwd shows one path and pwd -P another. Which is the real location?

    pwd -P. The plain pwd shows the path you typed, with the link in it. -P resolves links and prints the physical directory.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark