Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

which

Show the full path of the program that runs for a command name.

Level
Everyday Linux
Group
Shell and help
Needs root
No
Package
which

What it does

which searches the folders in your $PATH, from left to right, and prints the first file with that name. It answers "where is this program on the disk?".

On RHEL, which for Bash users is an alias that also shows aliases and functions (alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot').

To know which package a program comes from, use rpm -qf $(which NAME).

How to write it

which [OPTIONS] PROGRAM [PROGRAM ...]

Options 12

Most used

-a, --all
Print all matches in $PATH, not only the first one.
-i, --read-alias
Read aliases from standard input and report them too.
--skip-alias
Ignore --read-alias (use it to override the RHEL alias).
--read-functions
Read shell functions from standard input and report them too.
--skip-functions
Ignore --read-functions.
--skip-dot
Skip folders in $PATH that start with a dot.
--skip-tilde
Skip folders in $PATH that start with ~ and home folders.
--show-dot
If a $PATH folder starts with a dot, print ./PROGRAM.
--show-tilde
Print ~ instead of the full home folder path.
--tty-only
Stop reading options when the output is not a terminal (used by the RHEL alias).
-v, -V, --version
Show the version.
--help
Show help.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Find where a program is

    which passwd
    /usr/bin/passwd

    The program file that runs when you type passwd.

  2. 2See all copies of a program in $PATH

    which -a python3
    /usr/bin/python3
    /usr/local/bin/python3

    The first one wins. Two copies often explain "wrong version" problems.

  3. 3Find the package of a program

    rpm -qf $(which ss)
    iproute-6.x.x.el10.x86_64

    Now you know which package to update or reinstall.

  4. 4Check if a tool exists in a script

    which ansible >/dev/null 2>&1 && echo "ansible installed"

    The exit code is 0 when found, 1 when not found.

  5. 5Show the alias too (RHEL default)

    which ll
    alias ll='ls -l --color=auto'
    	/usr/bin/ls

    Because of the RHEL alias, which also shows aliases in an interactive shell.

  6. 6Use the plain program without the alias

    /usr/bin/which --skip-alias ll

    In a script the alias does not exist; this shows what a script would find.

  7. 7Check the path of a privileged program in a review

    which -a sudo

    Only /usr/bin/sudo should appear. Another sudo earlier in $PATH is a red flag.

  8. 8Find which shell a user would get

    which bash zsh fish
    /usr/bin/bash

    Only installed shells are printed; the others give an error message.

  9. 9Get the path for a cron job or a systemd unit

    which podman
    /usr/bin/podman

    cron and systemd use a short $PATH. Write the full path in ExecStart= and crontab lines.

  10. 10Check many tools at once before a deployment

    for t in git curl jq rsync; do which $t >/dev/null || echo "missing: $t"; done

    Prints only the tools that are not installed.

  11. 11Compare with type

    which cd; type cd

    which finds no file for cd (it is a built-in); type explains what it is.

Watch out

  • which only searches files in $PATH. It does not know built-ins like cd (use type).
  • sudo which NAME can give another answer, because root has a different $PATH.

Tips to remember

  • which = where is the file? type = what will run? rpm -qf = which package?

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man which