Fix 'bash: command not found' on Linux (Ubuntu 22.04 tested)
When you type a command and get 'command not found', it's usually a PATH issue or the command isn't installed. Here's how to fix it quick.
Simple fix (30 seconds): Check your PATH variable
Most of the time, 'command not found' means your shell can't find the program. The real fix is making sure your PATH includes the right directories. Type this in your terminal:
echo $PATHAfter hitting Enter, you should see a colon-separated list of directories. Something like:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binIf you see just a blank line or something like /home/yourname/bin, your PATH is broken. That's your problem.
To temporarily fix it, run:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binNow try your command again. If it works, you've found the issue. To make this permanent, add that line to your ~/.bashrc file. Use nano ~/.bashrc, paste it at the bottom, save with Ctrl+X, then Y, then Enter. Run source ~/.bashrc to apply it.
But if the command still fails, the program probably isn't installed. Move to the next fix.
Moderate fix (5 minutes): Install missing package
Linux distros split commands into packages. If you type ifconfig and get 'command not found', you need to install net-tools. Same for dig (needs dnsutils) or vim (needs vim).
First, figure out which package owns the command. On Debian/Ubuntu-based systems, use apt-file:
sudo apt update && sudo apt install apt-file -y
sudo apt-file update
apt-file search [command-name]For example, for dig:
apt-file search dig | head -5You'll see something like dnsutils: /usr/bin/dig. That tells you the package is dnsutils. Install it:
sudo apt install dnsutils -yOn RHEL/CentOS/Fedora, use yum whatprovides or dnf provides:
yum whatprovides */digThen install with sudo yum install bind-utils.
If you don't want to install tools, just Google 'which package provides [command] ubuntu' — it's faster. Common ones:
| Command | Package name (Ubuntu) | Package name (CentOS) |
|---|---|---|
| ifconfig | net-tools | net-tools |
| dig | dnsutils | bind-utils |
| nslookup | dnsutils | bind-utils |
| traceroute | traceroute | traceroute |
| vim | vim | vim-enhanced |
| htop | htop | htop |
After installing, test the command. If it still fails, check if the binary is in your PATH (see simple fix above).
Advanced fix (15+ minutes): Reinstall or rebuild the shell environment
If the command is installed but still not found, something's corrupted your shell environment. This happens when you mess with /etc/environment, ~/.bashrc, or ~/.bash_profile. I've seen it after failed updates or when someone deleted system files by accident.
Start by checking if the binary actually exists:
which [command]If it returns nothing, use locate or find to search:
sudo locate [command] | grep bin/Or:
sudo find /usr /bin /sbin -name [command] -type f 2>/dev/nullIf you find the binary, note its full path (e.g., /usr/local/bin/command). Then check if your PATH includes that directory. Run echo $PATH again. If not, add it (see simple fix).
If the binary is missing entirely, you may need to reinstall the package completely. On Ubuntu:
sudo apt purge [package-name] -y
sudo apt autoremove -y
sudo apt install [package-name] -yFor critical commands like ls, cp, or mv, you're in deeper trouble. These are part of coreutils. A missing ls means your system is broken. Boot from a live USB, mount your root partition, and reinstall coreutils:
sudo mount /dev/sda1 /mnt
sudo chroot /mnt
apt install --reinstall coreutilsReplace /dev/sda1 with your actual root partition.
One more thing — if you're using a custom shell like zsh or fish, check their config files too. Zsh uses ~/.zshrc, fish uses ~/.config/fish/config.fish. The same PATH rules apply.
If none of this works, you might have a damaged filesystem. Run sudo fsck on your root partition. I've seen a bad superblock cause 'command not found' on commands that were definitely installed. It's rare, but it happens.
Was this solution helpful?