GICT knowledge base
How to Fix and Prevent “command not found” Errors in Linux
Published
One of the most common issues you’ll encounter on a Linux system is the dreaded “command not found” error. You type a command, press Enter, and instead of your expected result, you see:
bash: foobar: command not found
This error can be confusing, especially if you’re sure the command should exist. Fortunately, once you understand what causes this message, you’ll be able to fix it quickly and avoid it in the future.
This guide explains what the “command not found” error means, why it happens, and how you can resolve it—step by step—with plenty of practical tips and examples.
What Does “command not found” Mean?
When you type a command in the terminal, Linux tries to find an executable file with that name in your system’s search paths (the PATH variable). If it can’t find the command, the shell returns the “command not found” error.
Common reasons include:
- The command’s package isn’t installed.
- The command is misspelled.
- The directory containing the command isn’t in your PATH.
- The executable is not marked as executable.
- You’re in the wrong working directory.
Step 1: Check for Typos
The most frequent cause is simply a typo. Double-check your spelling and case. Linux commands are case-sensitive, so LS and ls are not the same.
Example:
ls -l # not LS -l
Step 2: Make Sure the Command Is Installed
If you spell the command correctly and still see the error, the program might not be installed. Most Linux distributions provide helpful suggestions in the error message.
Example:
bash: htop: command not found
or on Ubuntu:
Command 'htop' not found, but can be installed with: sudo apt install htop
How to Find and Install the Package:
- Debian/Ubuntu:
sudo apt update sudo apt install htop - Fedora:
sudo dnf install htop - CentOS/RHEL:
sudo yum install htop
If you’re not sure what package provides the missing command, you can search for it:
- Debian/Ubuntu:
apt search <commandname> apt-file search bin/<commandname>(You may need to install
apt-file.) - Fedora:
dnf provides */<commandname>
Step 3: Check Your PATH Variable
Linux uses the PATH environment variable to know where to look for programs. If the directory where your command lives isn’t listed in PATH, you’ll get “command not found” even if the file exists.
How to Check PATH
echo $PATH
You’ll see something like:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
If your command’s location isn’t in one of these directories, the shell won’t find it.
How to Run a Command in the Current Directory
If you have an executable script or binary in your current folder, you need to prefix it with ./:
./myscript.sh
Otherwise, you’ll get:
bash: myscript.sh: command not found
How to Add a Directory to PATH
If you want Linux to always find your script or command, add its directory to your PATH. For example, if your scripts are in ~/bin:
nano ~/.bashrc
Add this line at the end:
export PATH="$PATH:$HOME/bin"
Save and reload:
source ~/.bashrc
Step 4: Make Sure the File Is Executable
If you wrote a script or downloaded a binary, it must be marked as executable.
chmod +x myscript.sh
Then, run with:
./myscript.sh
Step 5: Check for Missing Interpreter or Shebang Line
If your script starts with a shebang like #!/bin/bash or #!/usr/bin/python3, make sure the interpreter exists at that path.
If not, you may see errors like:
bash: ./myscript.py: /usr/bin/python3: bad interpreter: No such file or directory
Fix:
- Check the shebang line at the top of your script.
- Use
which python3orwhich bashto confirm the interpreter is installed and the path is correct.
Step 6: Use the Correct Shell
Some commands or scripts use syntax specific to Bash, Zsh, or another shell. If you try to run a script in the wrong shell, you might see “command not found” for otherwise valid commands.
Make sure you’re using the expected shell, or specify the shell explicitly:
bash scriptname.sh
Step 7: Verify Installation with which or type
You can check if a command is available and where it’s located with:
which commandname
or
type commandname
If these return nothing, the command isn’t installed or isn’t in your PATH.
Step 8: Check Permissions
If the command exists but isn’t executable by your user, you may get a “permission denied” error or sometimes “command not found.”
Check with:
ls -l /path/to/command
If needed, add execute permissions:
chmod +x /path/to/command
Step 9: Update the Shell or System
If you installed a package but the command still isn’t found, try logging out and back in, or run:
hash -r
This clears the shell’s command lookup cache. Or, reload your profile:
source ~/.bashrc
Step 10: Special Case – Aliases and Functions
If you expected a command to be available because you defined an alias or function, make sure the definition is in the correct shell startup file and has been loaded.
- For
Bash:add to~/.bashrc - For
Zsh:add to~/.zshrc
After editing, reload with:
source ~/.bashrc
or open a new terminal.
Practical Examples
Example 1: Installing a Missing Command
You try to use tree:
tree
bash: tree: command not found
Install it:
sudo apt install tree
Try again:
tree # Now it works!
Example 2: Running Your Own Script
You have a script called backup.sh in your home directory.
backup.sh
bash: backup.sh: command not found
Try:
./backup.sh # If you see "Permission denied": chmod +x backup.sh ./backup.sh
Example 3: PATH Issue with a Custom Program
You installed a program in /opt/myapp/ and want to run myapp from anywhere.
Add to PATH:
echo 'export PATH="$PATH:/opt/myapp"' >> ~/.bashrc
source ~/.bashrc
myapp
Example 4: Script with Wrong Interpreter
Your script starts with #!/usr/bin/python, but you only have Python 3 at /usr/bin/python3.
Edit the script’s first line to:
#!/usr/bin/python3
Save, make executable, and try again.
Tips to Prevent “command not found” in the Future
- Always check your spelling and case.
- Install new packages from your distribution’s repositories.
- When working with scripts, use
./script.shand ensure the file is executable. - Keep your PATH organized and avoid cluttering it with unnecessary directories.
- Add custom scripts to a dedicated
~/bindirectory and include it in your PATH. - Use the package manager to search for missing programs.
Conclusion
The “command not found” error is a common hurdle in Linux, but it’s almost always easy to solve once you know where to look. Whether it’s a missing package, a typo, permission issue, or a misconfigured PATH, there’s a straightforward fix. With the steps in this guide, you can confidently troubleshoot and resolve this error—and keep your Linux workflow running smoothly.
If you run into a stubborn “command not found” problem, retrace your steps methodically, check the basics, and don’t hesitate to search for documentation or ask for help in the Linux community. Every time you fix this error, you learn a little more about your system—and become a stronger Linux user in the process.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
