Tutorial

Diagnose and Fix a 'command not found' Error

Mister P
by  Mister P · on
Linux
Walk through every root cause of a Bash 'command not found' error — typo, broken $PATH, uninstalled package, missing execute bit — and apply the correct fix. Essential diagnostic skill for the RHCSA EX200 exam.

Diagnose and Fix a 'command not found' Error

Note

Before running any commands: click the + button in the terminal tab bar to open a new terminal tab. The playground history tracking activates in new sessions only. Commands run in the original tab will not register for task verification.

Every Linux admin eventually sees this:

bash: git: command not found

The message is the same regardless of cause — but the fix is different every time. This lab walks you through a systematic diagnostic sequence that works for every variant of this error. Master this flow and you will never stare at command not found and wonder where to start.

On the RHCSA exam you may encounter broken environments or need to install missing tools. This exact sequence applies.


What you'll practise

  1. Using type to detect typos and classify commands
  2. Inspecting and repairing $PATH
  3. Using which to locate executables on disk
  4. Using dnf provides to find and install missing packages
  5. Checking and fixing missing execute permissions

Step 1 — Check for a Typo Using type

Before assuming anything is broken, rule out a simple spelling mistake.

The type built-in asks Bash: "How would you run this word?" It searches aliases, functions, builtins, and then $PATH — in that order. If none of them match, Bash tells you immediately.

Run type against a deliberately misspelled command:

type tpye

You should see:

bash: type: tpye: not found

Bash found nothing matching tpye. Compare against the intended spelling, correct it, and retry.

Note

type is a Bash built-in — it does not spawn a subprocess and it cannot be silenced by a broken $PATH. That makes it the safest first diagnostic tool.


Step 2 — Confirm What type Reports for a Valid Command

Now run type against a command you know works, so you recognise each possible output:

type ls
OutputMeaning
ls is aliased to 'ls --color=auto'Alias — defined in your shell config; will work
ls is /usr/bin/lsExternal executable found in $PATH; will work
ls is a shell builtinBuilt into Bash; always works regardless of $PATH
bash: type: ls: not foundNothing found — continue to Step 3

Step 3 — Inspect $PATH for Missing Directories

If type returns not found for a command you believe is installed, the most common cause is a corrupted or overwritten $PATH.

Print it now:

echo $PATH

A healthy Rocky Linux 9 user $PATH looks like:

/home/laborant/.local/bin:/home/laborant/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

The critical directories are /usr/bin and /usr/sbin. If either is absent, commands like ls, cat, and grep will silently disappear.

Repair a broken $PATH

If /usr/bin is missing from your output, restore the default for this session:

export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:$HOME/.local/bin:$HOME/bin
Important

This export fixes $PATH for the current session only. On the RHCSA exam, if a question broke your PATH in a startup file (such as .bashrc), you must also fix the file — otherwise the next login will break it again.

Why does $PATH break?

The most common causes during an exam or on a misconfigured system:

  • Someone ran PATH=somedir instead of PATH=somedir:$PATH, wiping all previous entries
  • A .bashrc or /etc/profile.d/ script overwrites PATH unconditionally
  • The shell was started with env -i (empty environment)

If echo $PATH shows an empty string or only one directory, a script is almost certainly overwriting it.


Step 4 — Use which to Locate the Executable on Disk

If $PATH looks correct but the command is still missing, use which to search each $PATH directory explicitly:

which git

Two possible outcomes:

  • /usr/bin/git — the file exists on disk. The problem is likely a permissions issue. Jump to Step 6.
  • No output / no git in (...) — the file is not present anywhere in $PATH. Proceed to Step 5 to install it.
which returns nothing but I'm sure git is installed

Check whether git is installed somewhere outside your $PATH:

find /usr /opt /home -name git -type f 2>/dev/null

If it appears in a directory like /usr/local/bin that is absent from your $PATH, fix your PATH (Step 3) rather than reinstalling the package.


Step 5 — Check Whether the Package Is Installed

When the executable is nowhere on disk, you need to find and install the package that provides it.

dnf provides git

This queries the RPM database and available repositories for any package that owns a file or capability named git. Typical output:

git-2.43.0-1.el9.x86_64 : Fast Version Control System
Repo        : appstream
Matched from:
Provide    : git = 2.43.0-1.el9

Install the package:

sudo dnf install -y git
Note

dnf provides also accepts full paths. If you know the binary path but not the package, run dnf provides /usr/bin/git. This is useful when a man page or script references an exact path and you need to know which package to install.

dnf provides returns no match

The command name and the package name often differ. Try a broader search:

dnf search git

This searches package names and descriptions. The correct package may be git-core, perl-Git, or something else entirely.


Step 6 — Check Executable Permission on the File

This step covers a subtle failure: which finds the file, the package is installed, but running the command still fails.

The playground pre-created a script at /usr/local/bin/greet with the execute bit deliberately removed. Inspect it:

ls -l /usr/local/bin/greet

You should see something like:

-rw-r--r--. 1 root root 42 Jan 15 12:00 /usr/local/bin/greet

The permission string -rw-r--r-- has no x anywhere. Bash cannot execute this file even though it exists and is in your $PATH.

Restore the execute bit:

sudo chmod +x /usr/local/bin/greet
Note

chmod +x adds the execute bit for owner, group, and other simultaneously. On the RHCSA exam you may need finer control — for example chmod u+x adds it for the owner only. Know the difference.


Verification — Confirm the Command Resolves

After working through whichever steps applied to your situation, always end with type to confirm resolution:

type greet

Expected output:

greet is /usr/local/bin/greet

Bash now classifies greet as an external executable at a known path. Run it to confirm end-to-end:

greet

Expected:

Hello from greet!

Troubleshooting Reference

SymptomMost Likely CauseFix
type returns not found even after correcting spellingCommand not installed, not on $PATHdnf provides <command> then install
/usr/bin absent from echo $PATH output$PATH overwritten in current sessionexport PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:$HOME/.local/bin:$HOME/bin
which finds the file but shell returns Permission deniedExecute bit missingsudo chmod +x <full-path>
Command works as root but not as a regular user/usr/sbin not in user's $PATHRun with sudo, or add /usr/sbin to user's $PATH
dnf provides returns no matchPackage name differs from command namednf search <keyword>

Diagnostic Flow Summary

command not found
       │
       ▼
type <command>  ──── typo? ──────────────────► fix spelling, retry
       │
       │ not found
       ▼
echo $PATH ──── /usr/bin missing? ──────────► export PATH=..., retry
       │
       │ PATH looks correct
       ▼
which <command> ──── no path returned? ─────► dnf provides, install
       │
       │ path returned
       ▼
ls -l <path> ──── no execute bit? ──────────► chmod +x, retry
       │
       │ bit present
       ▼
type <command>  (should now resolve)

What to Remember for the Exam

  • type first, always. It is a builtin, immune to a broken $PATH, and tells you exactly how Bash resolves a name.
  • echo $PATH is your second move when type says not found. A wiped PATH silences almost every command at once.
  • dnf provides traces any missing file back to its RPM package. Works with command names and full paths.
  • chmod +x is the fix when the file exists but cannot be executed. The RHCSA exam occasionally presents scripts that need this step before they can run.

About the Author

Mister P

Mister P

Find this author online

More tutorials you might like

Learn by doing, not just by reading or watching

Sign up for a free account to start a VM playground right on this page, track your progress, and get notified about new learning materials.

Sign up for free