Before you start: what's happening
You typed sudo apt update or vim file.txt and bash replied: bash: command not found. That means the shell couldn't find the executable in any of the directories listed in your PATH variable. Either the command isn't installed, or the directory it lives in isn't in PATH.
This often happens after you edit .bashrc, or when you install something to a custom location. Or you might be in a minimal container or a fresh install that's missing common tools.
Work through the sections in order. Stop when the command works.
Section 1: The 30-second fix – check your PATH
First, see what your PATH currently is:
echo $PATH
You should see a colon-separated list like /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin. If those standard directories are missing, that's your problem.
Quick fix: set PATH manually (temporary)
Run this to add the usual places back:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
After that, try your command again. If it works, you've fixed it for this session. To make it permanent, you need to fix your shell config – that's in Section 2.
If the command still isn't found, move on to Section 2.
Section 2: The 5-minute fix – repair your shell config
Your shell reads ~/.bashrc (or ~/.profile, ~/.zshrc if you use zsh) at startup. If you accidentally set PATH to something weird there, that's the cause.
Open the file with a known-good editor like nano or vi. If you don't have those, try export with a built-in editor like ed – but that's painful. Simpler: use cat to view it first.
cat ~/.bashrc
Look for any line that sets PATH= – maybe something like PATH=/custom/bin without the standard directories. If you see that, comment it out or fix it to include the default directories.
If you can't edit the file because you don't have a text editor installed, use this one-liner to prepend the defaults:
echo 'export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' >> ~/.bashrc
Then reload the config:
source ~/.bashrc
Now test your command again. If it works, you're done. If not, you likely need to reinstall the missing package – that's Section 3.
Section 3: The 15+ minute fix – reinstall the missing package
When PATH is fine but the command still isn't found, the package isn't installed. This happens on minimal systems, or you might have removed it accidentally.
What to reinstall, depending on the command
sudo– packagesudoapt– packageapt(Debian/Ubuntu only)vim– packagevimorvim-tinyls,cp,mv– packagecoreutilsgrep– packagegrep
But here's the chicken-and-egg problem: if apt itself is missing, you can't use it to install anything. So you need to use a package manager's direct download or a static binary.
If apt exists but sudo doesn't
Switch to root (if you know the root password) or use su -:
su -
Then install sudo:
apt update && apt install sudo
If you don't have the root password but your user has sudo rights, you wouldn't be seeing this error. So you'll need to boot into single-user mode or recovery mode to reset. That's a longer story – search for 'reset root password ubuntu' if you need that.
If apt is missing entirely (Debian/Ubuntu)
This is rare but happens on stripped-down Docker images. You can download the apt .deb file manually using wget or curl – but those might also be missing. In that case, use busybox if you have it, or boot from a live USB and chroot into your system.
For a typical user, the easier route is to reinstall the whole package manager from a live USB. But before you go there, check if you have dpkg (Debian) or rpm (Red Hat). If you have dpkg, you can install apt from a downloaded .deb:
wget http://archive.ubuntu.com/ubuntu/pool/main/a/apt/apt_2.0.9_amd64.deb
sudo dpkg -i apt_2.0.9_amd64.deb
That's an example – pick the right version for your OS. Check your OS version with cat /etc/os-release first.
If vim is missing but you just want to edit files
Use nano if you have it, or vi (the minimal one) – often present even on slim systems. If not, you can use cat with a heredoc to write files, but that's tedious.
cat > myfile.txt << EOF
your text here
EOF
That's a workaround, not a fix. To actually get vim, follow the same download approach.
What if the command is in a non-standard location?
Some software installs to /opt or /usr/local but doesn't add itself to PATH. For example, if you installed a tool manually from source, it might end up in /usr/local/bin – which is usually in PATH already. But if you installed to /opt/mytool/bin, you need to add that path.
Find where the command is using find or locate:
find / -name vim -type f 2>/dev/null
If you get a path like /opt/vim/bin/vim, then add that directory to PATH:
export PATH=$PATH:/opt/vim/bin
Add that line to your ~/.bashrc to make it permanent.
Last resort: reinstall the system
If you've tried all this and still get the error, your system files might be corrupt. Back up your data and reinstall the OS. That's not ideal, but it's often faster than hunting down every missing binary.
Quick reference table
| Command | Typical package | Where it lives |
|---|---|---|
| sudo | sudo | /usr/bin/sudo |
| apt | apt | /usr/bin/apt |
| vim | vim | /usr/bin/vim |
| ls, cp, mv | coreutils | /bin or /usr/bin |
| grep | grep | /bin/grep |
Most people land in Section 1 or 2. Don't jump to reinstalling packages unless you've confirmed the command isn't in any PATH directory.