Troubleshooting

Fix "command not found" errors on Linux

Find out why bash says command not found: package not installed, wrong PATH, typos, sudo's secure_path, version-specific binaries and scripts with Windows line endings.

BeginnerLinux VPS3 min read

bash: xyz: command not found means the shell searched every directory in PATH and found no program with that name. Either it is not installed, it lives somewhere outside PATH, or the name is different. Here is how to fix command not found on Linux quickly.

Before you start

  • SSH access to the server.
  • The exact command you typed.

Check whether it exists at all

type xyz
which xyz

If nothing is found, search the disk (programs installed by hand often live in /usr/local/bin, /opt or your home directory):

find / -xdev -name 'xyz' -type f 2>/dev/null

Install the missing package

The package name often differs from the command name. Find which package provides it:

  • Ubuntu/Debian: the shell often suggests it; otherwise apt install apt-file && apt-file update && apt-file search bin/xyz.
  • AlmaLinux/Rocky: dnf provides '*/xyz'.
CommandPackage (Debian/Ubuntu)Package (RHEL family)
dig, nslookupdnsutilsbind-utils
netstat, ifconfignet-toolsnet-tools
killallpsmiscpsmisc
htophtophtop (EPEL)
semanagepolicycoreutilspolicycoreutils-python-utils
crontabcroncronie

Fix PATH

If the program exists but outside PATH, either call it with its full path or add its folder:

echo $PATH
export PATH="$HOME/.local/bin:$PATH"

Add the export line to ~/.bashrc to keep it. Typical cases:

  • pip install --user puts programs in ~/.local/bin.
  • npm install -g with a custom prefix, or nvm, puts Node tools under your home directory; nvm's PATH is only set in interactive shells where nvm is loaded.
  • Composer global tools live in ~/.config/composer/vendor/bin.

Works for you but not with sudo

sudo replaces PATH with a fixed secure_path from /etc/sudoers. Use the full path (sudo $(which node) app.js) or install the tool system-wide (e.g. into /usr/local/bin).

Works in SSH but not in cron or systemd

Cron and services use a minimal PATH and do not read .bashrc. Use absolute paths in crontabs and unit files, or set PATH= at the top of the crontab.

Version-specific names

  • python may not exist; use python3 (or install python-is-python3 on Ubuntu).
  • PHP versions: php8.3, with php chosen via update-alternatives --config php.
  • Docker Compose v2 is docker compose (a space), not docker-compose.

Script says "not found" but the file exists

./script.sh: /bin/bash^M: bad interpreter: No such file or directory

The script was saved with Windows line endings (CRLF). Convert it:

sed -i 's/\r$//' script.sh

Also check the shebang points to an interpreter that exists (#!/usr/bin/env bash is portable), and remember to run local scripts with ./script.sh, since the current directory is not in PATH.

Refresh the shell's command cache

After installing a program, bash may still remember an old location. Run hash -r or open a new shell.

Command not found FAQ

Why isn't the current directory in PATH?

For security: otherwise a malicious file named like a common command in a folder could run instead.

How do I see all directories searched?

echo $PATH | tr ':' '\n' lists them in order.

Can I install missing commands on cPanel hosting?

Not system-wide; you can place your own scripts in ~/bin if SSH access is enabled.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.