$_ CLI In Depth
Learning progress 0%
lesson filesystem inodes navigation

cd, pwd & the Working Directory

6 min read Module 2: Filesystem Navigation & Inodes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:23

The working directory belongs to the process

The kernel stores a current working directory for every process. When a program opens a relative path, the kernel resolves that path starting from this directory. The value is not a string kept by the program. It is a reference to an inode, and it stays valid even if the directory is later renamed or moved. If the directory is removed while a process sits in it, the process keeps the reference, and its relative paths continue to work until it asks for a path that goes outside the removed directory.

When you start a command, the child process inherits the working directory of the shell that launched it. Bash then passes its own environment to the child, and the child starts in the same place. Nothing that the child does with cd can affect the parent, because changing the directory is an action on the process itself. This is the reason a program cannot change your shell's directory for you. The shell has to make the change, and cd is a builtin for exactly that reason.

BASH3 lines
cd /workspace
bash -c 'cd /etc && pwd'
pwd

The inner bash changes to /etc and prints it. The outer shell still prints /workspace, because the inner shell changed only its own working directory. If you want a change that persists, you must run cd in the shell you are using, or source the script.

pwd and its two answers

The command pwd prints the working directory, but it can answer in two ways. By default, the bash builtin prints the logical path, which is the path as the shell understood it, including any symbolic links you passed through. The option -P prints the physical path, which is the real location after all links are resolved. The external program /bin/pwd behaves the same way with the same options. The logical path is stored in the variable PWD, and the physical path is what the kernel actually knows.

The difference is visible only when a link is involved. Create a link and move into it, then ask for both views. The logical view keeps the name you typed, which is usually what you want for prompts and for reading a path back. The physical view tells you where the files really are, which matters for backups, for comparing directories, and for any tool that must avoid following links.

BASH5 lines
cd /workspace
ln -sfn /etc shortcut
cd shortcut
pwd
pwd -P

The first pwd prints /workspace/shortcut. The second prints /etc. Both are correct answers to different questions.

cd and its options

With no argument, cd moves to $HOME. With a hyphen, it moves to $OLDPWD and prints the new location. Bash's cd accepts -L and -P. The default is -L, which keeps the logical path. The option -P asks for the physical location, which means that .. is then resolved against the real directory rather than against the text. If you are in a logical directory reached through a link and you use cd .., the shell may take you back up the name you typed, while cd -P .. takes you to the physical parent.

The variable CDPATH is a colon-separated list of directories that bash searches when a relative name does not start with ./ or /. It is convenient for jumping to a project from anywhere, but it also changes what a short name means, so scripts usually leave it unset. If bash uses a directory found through CDPATH, it prints the directory it changed to, which makes the effect visible.

Warning

Do not write cd dir && rm -rf * without checking that the directory change succeeded. If cd fails and the shell continues, the removal runs in the wrong directory. Write cd dir || exit 1 in scripts, so that a failed move stops the script.

Subshells keep a directory change contained

Parentheses start a subshell, which is a copy of the shell with its own working directory. A cd inside parentheses changes only the copy, so the directory of the outer shell is unchanged when the group ends. This is a clean way to run a few commands in another directory without the need to remember where you came from. A typical use is (cd build && make), where the make command runs inside the build directory and your prompt stays where it was.

Braces group commands without starting a subshell, so a cd inside braces changes the current shell. Choosing between the two is a common source of bugs. If you want the change to persist, use braces or no grouping at all. If you want it to disappear, use parentheses. Both forms return the exit status of the last command, which lets you chain them with || to stop on failure.

The same idea applies to pushd and popd, which keep a stack of directories that you can return to. They are convenient at a prompt, but a script is easier to read when it uses subshells and absolute paths, because a reader can see the directory of every command without tracing a stack.

Why scripts should avoid relying on the current directory

A script that uses relative paths works only from the directory where its author ran it. A common pattern is to find the directory of the script and move there first. The idiom cd "$(dirname "$0")" does that, although it has edge cases with symbolic links. An even safer habit is to build absolute paths from a variable that you set once, so every line of the script names its files without depending on where the script was started.

Practice in the lab

Use the lab to change directories, record where you are, and confirm the difference between the logical and physical views. Each lab step writes its result into a file in /workspace, because the checker cannot see the state of your shell. The shell state disappears when the step ends, so the file is the evidence that counts.

Resolving a path one component at a time

Visualizer

The kernel resolves a path left to right, one name at a time, starting at / for absolute paths or at the current directory for relative ones.

1 / 5
..
/
etc
/
.
/
passwd
Resolution
startcwd = /workspace
..parent → /
etclook up "etc" in / → /etc
.same directory → /etc
passwdlook up "passwd" in /etc → /etc/passwd

A relative path does not start with /, so resolution starts in the current working directory, /workspace.

cd /workspace; realpath ../etc/./passwd; ls -l ../etc/./passwd
Terminal lab · 3 steps · real Linux in your browser

Track the working directory

Record your directory, compare logical and physical paths through a link, and return with cd -.

01
Record where you are
Make a directory, move into it, and save the output of pwd in /workspace: ``bash cd /workspace mkdir -p demo/a cd demo/a pwd > /workspace/where.txt ``
Show hint for step 1
pwd prints the absolute path of the directory you are in.
02
Compare logical and physical paths
Create a symbolic link to the directory and move into the link. Save both views of the current directory: ``bash cd /workspace ln -sfn /workspace/demo/a shortcut cd shortcut pwd > /workspace/logical.txt pwd -P > /workspace/physical.txt ``
Show hint for step 2
The logical path keeps the name you used. The -P option resolves the link.
03
Return with cd minus
Go to /tmp, then return with cd -, and save the directory you are in: ``bash cd /workspace cd /tmp cd - pwd > /workspace/back.txt ``
Show hint for step 3
cd - goes back to the directory you were in before the last cd.
Check yourself Question 1 of 4
Concept Check Physical working directory recall

Which command prints the current directory with all symbolic links resolved?

Recap & Key Takeaways

Each process has its own working directory, and a child process cannot change its parent's directory.
pwd prints the logical path by default, and pwd -P prints the physical path after links are resolved.
cd - returns to the previous directory, which bash stores in OLDPWD.
Scripts should check that cd succeeds before they run commands that change files.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue