Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Use the which Command in Linux

Find the path of the program that runs when you type a command.

How-To Guides2 min read5 screenshots

What which does

which shows the full path of the executable that the shell would run for a command. It searches the folders in your PATH variable in order.

Syntax

which [options] [command]

Example:

which ls
Finding the location of the ls command

Output: /usr/bin/ls

Options

  • -a shows all the matches found in PATH, not only the first.
  • --version shows the version of which.
  • --help shows a summary of the usage.

Version

which --version
The which version

Help

which --help
The which help

Examples

Find one command

which grep
Finding the location of grep

Check several commands

which ls pwd cd
Finding several commands at once

cd is a shell built-in, so which may not find a file for it. Use type cd to learn what it is.

Show all matches

which -a python3

Use it in a script

#!/bin/bash
if which curl > /dev/null; then
    echo "curl is installed"
else
    echo "curl is not installed"
fi

command -v curl is a more portable way to do the same check.

which, whereis and type

  • which finds the executable in PATH.
  • whereis finds the binary, the source and the manual page.
  • type tells you if a name is an alias, a built-in, a function or a file.

Quick recap

  • which command prints the path of the command. Use which -a to see all matches.