Navigating with pwd, cd, ls, and tree
Filesystem navigation is not merely moving between folders. DevOps work depends on knowing exactly which directory a command will affect, which entries are hidden, and how to create evidence before changing files.
Learning objectives
By the end of this lesson
- Explain the current working directory and how processes inherit it.
- Navigate with pwd and cd using explicit, home-relative, and previous-directory targets.
- Interpret long listings, hidden entries, timestamps, ownership, and file-type indicators.
- Use tree or find to produce a bounded directory map.
- Build a repeatable course workspace and verify every navigation step.
1. Every process has a current working directory
A Linux path can be resolved relative to a process’s current working directory. Your interactive shell maintains its own working directory. Commands launched by that shell normally inherit it, which is why running the same relative command from two directories can affect different files.
flowchart TD S["Shell current directory"] --> R["Relative path argument"] R --> N["Kernel pathname resolution"] A["Absolute path argument"] --> N N --> I["Resolved filesystem object"] I --> C["Command reads or changes target"]
Before a command that copies, removes, changes permissions, installs, builds, or deploys, print and verify the working directory. A correct command in the wrong directory is still a failure.
2. Confirm location with pwd
pwd prints the shell’s current working directory. Bash
also maintains $PWD and $OLDPWD. The
logical view can preserve symbolic-link components, while the
physical view resolves them.
printf 'PWD variable: %s\n' "$PWD"
printf 'Logical path: '
pwd -L
printf 'Physical path: '
pwd -P
printf 'Previous directory: %s\n' "${OLDPWD-not set}"
pwdPrint current path using shell/default behaviorRoutine orientation
pwd -LLogical path, potentially retaining symbolic-link namesMatches the path through which the user navigated
pwd -PPhysical path with symbolic links resolvedVerifying the underlying directory target
3. Change location with cd
cd must be a shell built-in because an external process
cannot permanently change its parent shell’s working directory. The
command accepts absolute or relative paths and several useful
conventions.
cdYour home directoryEquivalent to a valid cd "$HOME"
cd /var/logAbsolute directoryIndependent of current location
cd ..Parent directoryResolved from the current location
cd -Previous working directoryUses $OLDPWD and normally prints the
destination
cd -- "$target"Directory stored in a variableQuoting and -- protect unusual names
start=$PWD
cd -- "$HOME"
printf 'Home: %s\n' "$PWD"
cd -- "$start"
printf 'Returned: %s\n' "$PWD"
# Bash directory-stack commands are useful for temporary jumps.
pushd /tmp >/dev/null
printf 'Temporary location: %s\n' "$PWD"
popd >/dev/null
printf 'Restored location: %s\n' "$PWD"
4. List directory entries with ls
ls shows names by default. Operational work often needs
hidden entries, ownership, permissions, sizes, timestamps, and
unambiguous escaping. Options can vary between implementations, so
use ls --help or man ls on the target
system.
# Names, including hidden entries other than . and ..
ls -A
# Long listing with human-readable sizes
ls -lhA
# Sort newest modification time first
ls -lt
# Show inode numbers and classify common file types
ls -liF
# Quote unusual names so whitespace and control characters are visible
ls -lb
A filename beginning with a dot is omitted by default from many listings. It has no special security protection. Configuration, credentials, caches, and repository metadata often use dot-prefixed names.
5. Read a long listing correctly
drwxr-xr-x 2 learner devops 4096 Aug 4 10:15 scripts
-rw-r----- 1 learner devops 842 Aug 4 10:14 deploy.env
lrwxrwxrwx 1 learner devops 11 Aug 4 10:16 current -> releases/v2
The first character distinguishes a directory, regular file, symbolic link, and other object types. The remaining characters summarize permission bits.
The next field counts hard links to the inode; directories reflect subdirectory relationships.
These identities interact with permission bits and access-control rules.
The meaning of size depends on object type. The displayed timestamp is usually modification time unless options select another.
Symbolic links display their stored target after an arrow.
6. Map a hierarchy with tree or find
tree is a convenient optional package, not guaranteed
to be installed. Limit depth and entry count when inspecting large
repositories or system directories.
target="$HOME/devops-academy"
if command -v tree >/dev/null 2>&1; then
tree -a -L 3 --dirsfirst -- "$target"
else
printf 'tree is unavailable; using find instead\n' >&2
find "$target" -maxdepth 3 -printf '%y %p\n' | sort
fi
A directory map is evidence, not a substitute for metadata. Use
stat, file, permissions tools, and content
inspection when the object’s exact nature matters.
7. Navigation patterns for automation
Guard every required cd
Use cd -- "$target" || exit in simple scripts, or
return an error from a function. Never continue silently from
the wrong directory.
Build paths from known variables
Use an established workspace root rather than chains of fragile
../../.. references.
Preserve whitespace and special characters
cd -- "$path" prevents splitting and option
confusion.
Print resolved context
Capture pwd -P, directory metadata, and target
lists before bulk operations.
8. Hands-on lab: build and map a course workspace
root="$HOME/devops-academy/linux/chapter03/lesson02"
mkdir -p -- \
"$root/projects/api/config" \
"$root/projects/web/public" \
"$root/runbooks" \
"$root/.state"
printf 'environment=lab\n' > "$root/projects/api/config/app.env"
printf '# Recovery runbook\n' > "$root/runbooks/recovery.md"
printf 'created=%s\n' "$(date --iso-8601=seconds 2>/dev/null || date)" > "$root/.state/manifest"
cd -- "$root" || exit 1
printf 'logical=%s\n' "$(pwd -L)"
printf 'physical=%s\n' "$(pwd -P)"
printf '\nTop-level entries, including hidden state:\n'
ls -lah
printf '\nBounded workspace map:\n'
if command -v tree >/dev/null 2>&1; then
tree -a -L 4 --dirsfirst .
else
find . -maxdepth 4 -printf '%y %p\n' | sort
fi
printf '\nRound trip with cd -:\n'
cd projects/api
pwd
cd -
pwd
Verification checklist
9. Common navigation mistakes
Running a destructive command after a failed cd
If navigation fails and a script continues, a relative target can resolve in the old directory. Stop immediately on required navigation failure.
Assuming ls shows everything
Dot-prefixed entries are omitted unless requested. Important repository and configuration state may be hidden.
Parsing default ls output in scripts
Human-oriented formatting and unusual filenames make it
unreliable. Prefer find, shell globs, or
machine-readable interfaces.
Using deep relative paths without context
Long chains of parent references are difficult to review. Establish a known root and derive targets from it.
10. Knowledge check
Question 1. Why must cd be
implemented by the shell?
Question 2. What is the difference between
pwd -L and pwd -P?
Question 3. Why is a default
ls listing insufficient before many operations?
11. Summary
The shell’s working directory is inherited by commands and controls
how relative paths resolve. Use pwd to establish
context, cd with checked and quoted targets,
ls to inspect metadata and hidden entries, and
tree or bounded find output to map a
hierarchy. Verification belongs before every consequential file
operation.
12. Further reading
- GNU Coreutils documentation for pwd, ls, and directory operations.
- GNU Bash Reference Manual — Bourne shell built-ins, directory stack, variables, and shell parameters.
- Linux manual pages for chdir, getcwd, stat, find, and tree when installed.
- Filesystem Hierarchy Standard — conventional system directory purposes.
- POSIX specifications for cd, pwd, and pathname resolution.
Keep the academy open
Support free, practical DevOps education.
Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0
Send only Ethereum/ERC-20 compatible assets to this
address.