1
BashERRORNotableFilesystemHIGH confidence

No such file or directory

What this means

This error message, usually with exit code 1, indicates that a path specified in a command does not exist. The file or directory is missing from the location the command is looking.

Why it happens
  1. 1A typo in the file or directory name.
  2. 2The file was moved or deleted.
  3. 3The script is being run from a different working directory than expected, causing relative paths to be incorrect.
How to reproduce

Trying to `cd` into a directory that does not exist.

trigger — this will error
trigger — this will error
#!/bin/bash
cd /nonexistent-directory
echo "Exit: $?"

expected output

bash: cd: /nonexistent-directory: No such file or directory
Exit: 1

Fix 1

Check for existence before use

WHEN A file or directory might not exist

Check for existence before use
FILEPATH="/path/to/file"
if [ -f "$FILEPATH" ]; then
    echo "File exists."
else
    echo "File does not exist." >&2
fi

Why this works

The `test` command (`[ ]`) with operators like `-f` (file), `-d` (directory), or `-e` (exists) can be used to verify a path before using it.

Fix 2

Use absolute paths

WHEN A script may be run from different locations

Use absolute paths
source "/opt/my-app/config.sh"

Why this works

Absolute paths starting with `/` are not dependent on the current working directory, making scripts more reliable.

What not to do

Create the file or directory just to silence the error

This can hide the real problem, which might be that a different, necessary process failed to create the file earlier.

Sources
Official documentation ↗

GNU Bash Manual — conditional expressions

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Bash errors