How the Shell Finds Commands: PATH Explained
When you type ls and press Enter, how does the shell know where the ls program lives on disk? It searches the directories listed in your PATH environment variable, in order, until it finds an executable file with that name. PATH is one of the most important variables on your system — and the most common cause of “command not found” errors.
See your current PATH
echo $PATH
# typical output:
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/you/.local/bin
That’s a colon-separated list. The shell searches each directory in order — first match wins.
Find where a command actually lives
which ls # /usr/bin/ls
which python3 # /usr/bin/python3
which docker # /usr/bin/docker (or wherever)
# Show ALL matches in PATH (catches conflicts)
which -a python
# More info, including aliases and shell builtins
type ls
type cd
which only shows the first match. Use type -a or which -a to find conflicts (same command in multiple PATH directories).
Standard PATH directories
| Directory | Purpose |
|---|---|
/bin |
essential commands (ls, cp, mv) — usable when only / is mounted |
/sbin |
essential admin commands (mount, fdisk) |
/usr/bin |
most user programs |
/usr/sbin |
most admin programs |
/usr/local/bin |
manually installed (not from package manager) |
~/.local/bin |
per-user installs (pip install –user, etc.) |
Add a directory to PATH
Append (lowest priority — system commands win):
export PATH="$PATH:/opt/myapp/bin"
Prepend (highest priority — your override wins):
export PATH="/opt/myapp/bin:$PATH"
Make it permanent — add the line to ~/.bashrc (bash) or ~/.zshrc (zsh). The change takes effect for new shells, or run source ~/.bashrc to reload.
Run a command not in PATH
Use the full path or a relative path. The leading ./ matters:
/opt/tools/myscript.sh # absolute path
./myscript.sh # relative path (same dir)
Why ./? For security. The current directory is NOT in PATH on Linux (unlike Windows), so a malicious file named ls in a folder you cd into won’t run when you type ls.
Common PATH problems
“command not found” but the file exists
- The directory containing it is not in PATH. Run
echo $PATHto check. - The file is not executable. Run
chmod +x file.
Wrong version of a command runs
Two versions of Python or node in PATH; the first one wins. Check with which -a python. Reorder PATH so the right one comes first.
Sudo can’t find a command you can run
Sudo uses a restricted PATH for security. Use the full path with sudo:
sudo /home/you/.local/bin/myscript
What to learn next
PATH is one of many environment variables. The next node covers the broader system of environment variables and how to manage them.