Linux Error Guide: 'bash: cd: No such file or directory' — Fix a Missing Directory Path
Fix 'bash: cd: No such file or directory' when a target directory is missing, mistyped, or on the wrong path. Spot typos, dead symlinks, and hidden chars.
- #linux
- #troubleshooting
- #errors
- #bash
Stuck on this Linux Admins error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Overview
Few messages are more common — or more deceptively simple — than the shell telling you it cannot enter a directory. You run cd, and Bash immediately rejects the path:
bash: cd: /opt/app/releases/current: No such file or directory
This is Bash reporting that the path you handed to cd does not resolve to an existing directory. It is a lookup failure, not a permission failure. The distinction matters: No such file or directory means the final component (or something above it) does not exist as far as the filesystem can tell, while Permission denied means the path exists but you lack the rights to traverse it. Most of the time the cause is mundane — a typo, a stale path, a relative path run from the wrong working directory, or a broken symlink — but in automation it can silently derail an entire deploy script. This guide walks through the symptoms, the root causes, and a repeatable diagnostic workflow to pin down exactly why the path won’t resolve.
Symptoms
- Running
cd /some/pathreturnsbash: cd: /some/path: No such file or directoryand leaves you in the current directory. - A shell script aborts (or, worse, silently continues in the wrong directory) at a
cdline. - Tab-completion fails to complete the path, hinting the component doesn’t exist.
- The error appears for a path that “was there yesterday” — a sign the directory was moved, deleted, or lived on an unmounted volume.
- A
cdinto a symlink fails even thoughlsshows the symlink itself. - The same command works when you type it manually but fails inside a script (a strong hint at variable/quoting or working-directory issues).
Common Root Causes
- Typos — a transposed or missing character in the path (
/var/lgoinstead of/var/log). - Relative vs. absolute confusion —
cd releasesworks only from the right parent;cd /releasestargets the filesystem root. - Deleted or moved directory — the target no longer exists at that location.
- Not-yet-created directory — a script assumes a path that an earlier step was supposed to create.
- Dead symlink — the symlink exists but its target was removed or renamed.
- Unquoted variables in scripts —
cd $DIRwhere$DIRis empty or contains spaces. - Trailing whitespace or newline — a path built from command output that carries an invisible
\nor space. - Wrong working directory — a relative
cdexecuted from an unexpectedpwd. - Unmounted filesystem — the mount point exists but the volume backing it isn’t mounted (the expected subdirectories are absent).
Diagnostic Workflow
Start by confirming what actually exists. Use ls -ld on the exact path — the -d flag lists the directory entry itself rather than its contents, so you see whether it resolves at all:
ls -ld /opt/app/releases/current
If that fails, walk up the tree one component at a time to find where the chain breaks:
ls -ld /opt
ls -ld /opt/app
ls -ld /opt/app/releases
The first level that reports No such file or directory is your culprit. Next, resolve the path canonically. realpath prints the absolute, symlink-resolved path and errors out on a missing component; readlink -f does the same and is handy for following symlink chains:
realpath /opt/app/releases/current
readlink -f /opt/app/releases/current
To inspect a symlink without following it, use ls -l (note the -> target) or readlink without -f:
ls -l /opt/app/releases/current
readlink /opt/app/releases/current
If readlink prints a target but ls -ld on that target fails, you have a dangling symlink. Confirm your current location with pwd before running any relative cd, and use stat for a fuller picture of an entry:
pwd
stat /opt/app/releases
When a path looks correct but still fails — especially one assembled from variables or command substitution — suspect hidden characters. cat -A renders line endings and non-printing characters: $ marks end-of-line, ^I marks a tab, and M- sequences flag high-byte bytes:
printf '%s' "$TARGET_DIR" | cat -A
A trailing $ on its own line, or ^I, reveals whitespace baked into the variable. Finally, rule out permissions: if ls -ld on the parent shows the directory exists but you get Permission denied on traversal, that’s a mode/ownership problem (fix with the right chmod/chown or sudo), not the “No such file or directory” case this guide covers.
Example Root Cause Analysis
A deploy script fails at midnight with a cd error. The relevant line is cd "$RELEASE_DIR", where RELEASE_DIR is derived from the latest release tag:
RELEASE_DIR="/opt/app/releases/$(cat /opt/app/CURRENT_TAG)"
cd "$RELEASE_DIR"
The failure:
bash: cd: /opt/app/releases/v2.4.1
: No such file or directory
Notice the path wraps onto a second line before No such file or directory — a tell-tale sign of an embedded newline. Confirm what the directory actually contains and what the variable holds:
$ ls -ld /opt/app/releases/v2.4.1
drwxr-xr-x 4 deploy deploy 4096 Jul 6 23:58 /opt/app/releases/v2.4.1
$ printf '%s' "$RELEASE_DIR" | cat -A
/opt/app/releases/v2.4.1$
The real directory exists, but the variable ends with $ on its own line under cat -A — the value carries a trailing newline. The CURRENT_TAG file was written with a trailing \n, and $(cat ...) normally strips trailing newlines during command substitution — but here the file contained v2.4.1\n\n (a blank line), so one newline survived. The fix is to strip whitespace explicitly. Reading with read, or trimming via parameter expansion, both work:
read -r TAG < /opt/app/CURRENT_TAG
RELEASE_DIR="/opt/app/releases/${TAG}"
cd "$RELEASE_DIR" || exit 1
Re-running the script now enters the directory cleanly. The root cause was not a missing directory at all — it was an invisible newline appended to an otherwise-valid path.
Prevention Best Practices
- Always quote variable paths:
cd "$DIR", nevercd $DIR, to survive spaces and empties. - Fail fast in scripts: chain
cd "$DIR" || exit 1(or runset -euo pipefail) so a badcdstops execution instead of continuing in the wrong place. - Trim command-substitution output with
read -ror"${var%$'\n'}"before using it in a path. - Prefer absolute paths in automation to avoid working-directory surprises.
- Create-then-enter: use
mkdir -p "$DIR" && cd "$DIR"when a script owns the directory’s lifecycle. - Validate before use: guard with
[ -d "$DIR" ]and emit a clear error if it fails. - Verify symlink targets in deploy flows with
readlink -fbefore switching thecurrentlink. - Check mounts for network/attached volumes with
mountpoint -q "$DIR"before assuming the tree is present.
Quick Command Reference
ls -ld /path/to/dir # Does the directory entry exist?
ls -ld /path /path/to /path/to/dir # Walk up to find the broken component
realpath /path/to/dir # Canonical absolute path (errors if missing)
readlink -f /path/to/dir # Resolve symlink chain to final target
readlink /path/to/link # Show a symlink's immediate target
ls -l /path/to/link # See the symlink '->' target
pwd # Confirm working directory before relative cd
stat /path/to/dir # Full metadata for the entry
printf '%s' "$DIR" | cat -A # Reveal trailing newlines/tabs/spaces
[ -d "$DIR" ] && cd "$DIR" || echo "missing: $DIR" # Guarded change
mkdir -p "$DIR" && cd "$DIR" # Create then enter
mountpoint -q "$DIR" && echo mounted # Is the filesystem actually mounted?
Conclusion
bash: cd: No such file or directory almost always means exactly what it says — the path doesn’t resolve — but the reason ranges from a one-character typo to an invisible newline smuggled in through command substitution. Walk the path upward with ls -ld, canonicalize it with realpath or readlink -f, and reach for cat -A the moment a “correct-looking” path keeps failing. Bake in quoting, || exit 1 guards, and mkdir -p so your scripts fail loudly and predictably instead of drifting into the wrong directory.
Fixed it? Get 500 Linux Admins & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.