No such file or directory
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.
- 1A typo in the file or directory name.
- 2The file was moved or deleted.
- 3The script is being run from a different working directory than expected, causing relative paths to be incorrect.
Trying to `cd` into a directory that does not exist.
#!/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
FILEPATH="/path/to/file"
if [ -f "$FILEPATH" ]; then
echo "File exists."
else
echo "File does not exist." >&2
fiWhy 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
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.
✕ 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.
GNU Bash Manual — conditional expressions
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev