How to Fix 'No Such File or Directory' Error in Linux
In this tutorial, you'll learn about How to Fix 'No Such File or Directory' Error in Linux. We cover key concepts, practical examples, and best practices.
The Problem
You try to run a script or access a file and get:
bash: ./script.sh: No such file or directory
or
ls: cannot access 'data.csv': No such file or directory
Quick Fix
1. Check the file exists
ls -la /path/to/file
If it doesn't exist, you're in the wrong directory or the file was never created.
2. Check your current directory
pwd
Navigate to the correct directory:
cd /home/user/project/
ls
3. Wrong shebang (common with scripts)
The file exists but the interpreter doesn't. Check the first line:
head -1 script.sh
If it says #!/usr/bin/python but Python is at /usr/bin/python3, fix it:
sed -i '1s/python/python3/' script.sh
4. Broken symlink
The file is a symlink pointing nowhere:
file broken-link
Output: broken-link: broken symbolic link to /nonexistent/path
Recreate the symlink:
ln -sf /real/path broken-link
Prevention
Always use absolute paths in scripts. Check for broken symlinks after moving directories. Use file command to inspect what you're dealing with.
Common Causes
| Cause | Symptom | Fix |
|---|---|---|
| File doesn't exist | ls shows nothing |
Create or locate the file |
| Wrong directory | pwd shows unexpected path |
cd to correct directory |
| Wrong shebang | Script exists but won't run | Fix interpreter path with sed |
| Broken symlink | file shows "broken" |
Recreate with ln -sf |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro