GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 📚 Books
  • 🎓 Courses
  • 👩‍💻 Resources
  • ✨ Latest Tips and Tutorials
  • 📬 Newsletter
  • 🪪 Who we are?

This view

  • Original page
  • New address

GeezerVison is showing linuxhandbook.com — Which Command in Linux [Explained with Examples]

  • File Management Commands in Linux
  • Basic File Commands

    • touch Command Examples
    • ls Command Examples
    • cp Command Examples
    • mv Command Examples
    • ln Command Examples
    • rm Command Examples
  • Directory Operation Commands

    • cd Command Examples
    • mkdir Command Examples
    • rmdir Command Examples
  • File Searching Commands

    • which Command Examples
    • locate Command Examples
    • xargs Command Examples
    • find Command Examples
File Management Commands in Linux
File Management Commands in Linux
File Management Commands in Linux
File Permission and Property Commands in Linux File Viewing and Text Processing Commands in Linux Process Management Commands in Linux Disk Management Commands Networking Commands in Linux User and Group Management Commands in Linux
Warp Terminal

If you are wondering where exactly is a certain program is located, simply use which on it. The which command locates an executable file in your shell’s search path.

This Linux command has a simple syntax:

which [-a] filename

Let’s see how to use this simple but useful command.

Linux which command examples

Let’s say you want to know where is the Java executable, use which command:

which java

The output could be like this:

abhishek@linuxhandbook:~$ which java
/usr/bin/java

Note that which only works on executable files. So you should use it only with the argument that you can run. For example, you install Java through the JDK package but you don’t run a command called ‘jdk’, you run ‘java’. So you use which command on java, not jdk.

If the which command doesn’t find the executable in the current path, it returns nothing.

Using which command with multiple executable files

You can provide more than one argument to which command:

which man java python nada

The output for me was:

abhishek@linuxhandbook:~$ which man java python nada
/usr/bin/man
/usr/bin/java
/usr/bin/python

Did you notice something here? I gave it four arguments but the result is displayed for three of them only. It’s because ‘nada’ is not an executable. There is no output for that.

Display all pathnames with which command

The which command in Linux has only one option, -a. By default, which command prints only one pathname for its arguments.

If a program has executables in two places, say in /usr/bin/program and in /usr/local/bin/program, you can display both pathnames using the -a option.

which -a <program_name>

For example, here are the locations of all the sudo executables:

abhishek@ubuntu-25-10:~$ which -a sudo
/usr/bin/sudo
/bin/sudo

Exit status of which command

If you use which command in a bash script, you may need to know its exit status.

Which command has the following exit status:

  • 0 – all arguments are found and executable
  • 1 – one or more arguments is nonexistent or non-executable
  • 2 – if an invalid option is specified

That’s all you need to know about which command in Linux. If you have questions or suggestions, do let me know in the comments below.

Updated on Sep 9, 2025
Share to X Share to Facebook Share to Linkedin
Copied
Previous
Next

About the author

Abhishek Prakash Abhishek Prakash

Creator of Linux Handbook and It's FOSS. An ardent Linux user who has new-found love for self-hosting, homelabs and local AI.

View profile

On this page

Linux for DevOps

A comprehensive Linux course for students looking to make a career in DevOps.

Start here
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.