Real-world examples of resolving file not found error in Linux
Let’s start with a few quick, realistic scenarios. These examples of resolving file not found error in Linux pop up constantly:
- A script fails with
/bin/bash^M: bad interpreter: No such file or directoryafter being edited on Windows. python3 script.pyexplodes because the script imports a module from a hard-coded path that doesn’t exist on the server.- A systemd service fails because its
ExecStartpath is wrong. - A Docker container says
No such file or directoryeven though the file is clearly in the image.
Each of these is a different flavor of the same thing: Linux is looking for a path that either doesn’t exist, doesn’t exist there, or isn’t executable in the way you think it is.
Below are the best examples I’ve seen in real environments, plus how to fix them without wasting an afternoon.
Classic example of a simple typo in the path
You run this:
tail -f /var/log/ngnix/access.log
Linux answers:
tail: cannot open '/var/log/ngnix/access.log' for reading: No such file or directory
You swear Nginx is installed. You’re right. Your spelling isn’t.
What’s happening
The directory is actually /var/log/nginx, not /var/log/ngnix. One letter off is enough to trigger the error.
How to confirm
ls /var/log/ng*
You’ll see /var/log/nginx but not /var/log/ngnix.
How to fix it
Use tab completion religiously:
tail -f /var/log/nginx/access.log
This is one of the simplest examples of resolving file not found error in Linux: verify the path with ls, use tab completion, and stop trusting your muscle memory.
Example of wrong working directory in scripts and cron jobs
You have a script:
#!/bin/bash
python3 process_data.py data/input.csv
It runs fine when you execute it manually from the project directory. But from cron, you see:
python3: can't open file 'process_data.py': [Errno 2] No such file or directory
What’s happening
Cron doesn’t run your script from your project directory. Its working directory is usually your home directory or /. So process_data.py and data/input.csv are not found.
How to confirm
Add this to the top of your script:
echo "PWD is: $(pwd)" >> /tmp/debug.log
Check /tmp/debug.log after cron runs. You’ll see it’s not your project path.
How to fix it
Use absolute paths instead of relative ones:
#!/bin/bash
cd /home/ubuntu/myapp || exit 1
python3 /home/ubuntu/myapp/process_data.py /home/ubuntu/myapp/data/input.csv
Or set WorkingDirectory= in a systemd timer/service instead of cron.
This is one of the best examples of examples of resolving file not found error in Linux: when automation is involved, always assume the working directory is wrong until you prove otherwise.
Windows line endings: /bin/bash^M: bad interpreter
You clone a repo on Windows, edit a script in a Windows editor, then run it on Linux:
./deploy.sh
Linux responds:
-bash: ./deploy.sh: /bin/bash^M: bad interpreter: No such file or directory
You check /bin/bash and it absolutely exists. So what gives?
What’s happening
The script uses Windows-style line endings (\r\n), and the shebang line actually points to /bin/bash
. That invisible ^M (carriage return) makes Linux look for /bin/bash^M, which does not exist.
How to confirm
file deploy.sh
You’ll see something like:
deploy.sh: Bourne-Again shell script, ASCII text, with CRLF line terminators
How to fix it
Convert line endings to Unix style:
dos2unix deploy.sh
Or using sed if dos2unix isn’t installed:
sed -i 's/\r$//' deploy.sh
Going forward, configure your editor or Git to use LF on Linux-targeted scripts. Many teams document this in their internal coding standards; for a broader discussion on text encodings and portability, the GNU documentation is a solid reference.
Broken or stale symlink: the file moved, the link didn’t
You list a directory and see a config file:
ls -l /etc/myapp/config.yml
Output:
lrwxrwxrwx 1 root root 24 Jan 1 10:00 /etc/myapp/config.yml -> /opt/myapp/config.yml
But your app logs:
Error: cannot open /etc/myapp/config.yml: No such file or directory
What’s happening
The symlink points to /opt/myapp/config.yml, but that file was removed or moved during an upgrade. The path in the symlink is now invalid.
How to confirm
ls -l /opt/myapp/config.yml
If you see:
ls: cannot access '/opt/myapp/config.yml': No such file or directory
then the link is broken.
How to fix it
Point the symlink at the right file:
ln -sf /opt/myapp/config/config.yml /etc/myapp/config.yml
Or, if you no longer need a symlink, replace it with a real file.
This is one of the more subtle examples of resolving file not found error in Linux: the filename you see in ls is not the file your program opens; the real path is whatever the symlink points to.
systemd service with wrong ExecStart path
You create a service:
[Service]
ExecStart=/usr/local/bin/myapp --config /etc/myapp/config.yml
Then:
sudo systemctl start myapp
sudo systemctl status myapp
And you see:
myapp.service: Failed at step EXEC spawning /usr/local/bin/myapp: No such file or directory
What’s happening
The binary isn’t actually at /usr/local/bin/myapp. Maybe your build pipeline installed it to /usr/bin/myapp instead.
How to confirm
which myapp
## or
ls -l /usr/local/bin/myapp /usr/bin/myapp
You’ll discover the real location.
How to fix it
Update the service file:
ExecStart=/usr/bin/myapp --config /etc/myapp/config.yml
Then reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart myapp
This is a textbook example of examples of resolving file not found error in Linux in modern production setups, where systemd is the gatekeeper for almost everything.
Docker container: file exists on host, not in the image
You build a container and run:
docker run myimage /app/run.sh
Inside the container, you see:
/bin/sh: 1: /app/run.sh: not found
On your host machine, /app/run.sh is clearly there in your project directory.
What’s happening
The file exists on your host, but it was never copied into the image, or it’s at a different path inside the container. The container filesystem is not your host filesystem.
How to confirm
Inspect the image:
docker run -it --rm myimage ls -R /
Or more specifically:
docker run -it --rm myimage ls -l /app
How to fix it
Make sure your Dockerfile copies the script to the correct path:
WORKDIR /app
COPY run.sh ./
RUN chmod +x run.sh
CMD ["/app/run.sh"]
Then rebuild:
docker build -t myimage .
Containerization has made this one of the most common modern examples of resolving file not found error in Linux: the path inside the container must match what your CMD or ENTRYPOINT expects.
Python virtual environments and missing interpreter
You have a Python script with a shebang:
#!/home/ubuntu/venv/bin/python
You move the project to another server where the virtualenv lives at /srv/venv. Now running the script gives:
./script.py
-bash: ./script.py: /home/ubuntu/venv/bin/python: bad interpreter: No such file or directory
What’s happening
The shebang hard-codes the old virtualenv path.
How to confirm
Check the first line:
head -n 1 script.py
How to fix it
Use env in the shebang so it finds the interpreter from your PATH:
#!/usr/bin/env python3
Then activate the virtualenv before running the script:
source /srv/venv/bin/activate
./script.py
This is one of the best examples of examples of resolving file not found error in Linux when code is deployed across multiple servers with different layouts.
Case-sensitivity: works on macOS, fails on Linux
On your macOS laptop, you run:
python3 app.py
Inside app.py:
from utils.Logger import Logger
On Linux, you get:
ModuleNotFoundError: No module named 'utils.Logger'
Even though utils/logger.py exists.
What’s happening
Many macOS setups use case-insensitive filesystems by default. Linux is case-sensitive. Logger and logger are different names.
How to confirm
On Linux:
ls utils
You’ll see logger.py, not Logger.py.
How to fix it
Make the import match the actual filename:
from utils.logger import Logger
Or rename the file to match the import. This is a cross-platform example of resolving file not found error in Linux that bites teams migrating from laptops to real servers.
CI/CD pipeline: artifact path mismatch
In your CI pipeline (GitHub Actions, GitLab CI, Jenkins, pick your poison), a step runs:
./build/output/myapp
The job log says:
./build/output/myapp: No such file or directory
But the previous step clearly prints:
ls build/
## output:
## bin logs
What’s happening
The build now writes artifacts to build/bin/myapp instead of build/output/myapp. The pipeline script wasn’t updated.
How to confirm
Add a diagnostic step:
ls -R
pwd
You’ll see where the artifacts really are.
How to fix it
Update the path in the pipeline config:
./build/bin/myapp
Or better, parameterize the artifact directory in one place so future refactors don’t scatter path changes everywhere.
This is a modern, pipeline-focused example of examples of resolving file not found error in Linux that shows up as soon as teams start automating builds.
General strategy: turning “No such file or directory” into a quick fix
Across all these real examples of resolving file not found error in Linux, the debugging pattern is almost always the same:
- Verify the exact path you’re using with
ls -lorstat. - Check the working directory with
pwdand use absolute paths in scripts. - Inspect shebang lines for wrong interpreters or Windows line endings.
- Follow symlinks with
ls -land confirm the target exists. - Confirm inside containers rather than assuming host paths exist there.
For deeper reading on Linux filesystems and path behavior, the Linux Documentation Project and the GNU Coreutils manual are reliable references maintained by long-standing open-source communities.
FAQ: common questions and examples
Q: What are some quick examples of resolving file not found error in Linux for beginners?
A few easy wins:
- Use
ls /path/to/fileto confirm the file actually exists. - Use tab completion to avoid typos in directory names.
- Replace relative paths (
./data/input.csv) with absolute paths (/home/user/project/data/input.csv) in scripts.
Q: Can environment variables cause a “file not found” situation?
Yes. If you rely on variables like \(PATH, \)HOME, or custom variables such as $APP_CONFIG, and they’re unset or different in a non-interactive shell (cron, systemd), the resolved path can be wrong or empty, leading to No such file or directory.
Q: Is there an example of this error being caused by missing execute permissions instead of a missing file?
Sometimes you see:
bash: ./script.sh: Permission denied
Instead of No such file or directory. That’s a different issue: the file exists, but isn’t executable. Fix it with:
chmod +x script.sh
If you do see No such file or directory, but ls shows the file, check for the Windows ^M case or a missing interpreter referenced in the shebang.
Q: How can I systematically avoid these errors in a team setting?
Standardize on:
- Absolute paths in automation.
#!/usr/bin/envshebangs for interpreters.- LF line endings for scripts.
- Consistent directory layouts across environments.
Many organizations treat these as part of their internal engineering guidelines, similar in spirit to how health agencies like the CDC publish clear, repeatable best practices—just applied to code instead of public health.
Q: Are there tools that help detect broken paths or missing files in Linux projects?
Static analysis tools and linters for languages like Python or JavaScript can catch some missing imports or incorrect paths. For shell scripts, tools like shellcheck can warn about suspicious path usage. For configuration management, systems like Ansible or Puppet can verify file presence before use.
Across all of these, the theme is the same: make path handling explicit and test it early, so examples of resolving file not found error in Linux remain short, boring stories instead of 3 a.m. outages.
Related Topics
Real-world examples of file not found error examples in Node.js
Practical examples of file not found error examples in Java (and how to fix them)
The best examples of file not found error examples in HTML linking (and how to fix them)
Real-world examples of resolving file not found error in Linux
Modern examples of File Not Found exception handling in real code
Examples of File Not Found Error in Python: 3 Practical Examples (Plus More You Actually See)
Explore More File Not Found Errors
Discover more examples and insights in this category.
View All File Not Found Errors