FactorPad
Build a Better Process

Linux which Command Summary with Examples

Linux which allows you to locate commands.
  1. Purpose - Learn what which is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with which.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this video on Linux which, 18 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / which-command.html


An ad-free and cookie-free website.


The Linux which command

Beginner

Learn to find command locations at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux which Command Summary with Examples (3:36)

Video Script

The Command and Why You Need It

Our eighteenth word, or command to memorize is which from the category Help.

which allows you to locate commands.

Common Linux which Options
-options description
-a Print all matches

Recall from the last video (tutorial) how we saw two versions of the file command in the Linux filesystem? Well, I wanted to use the which command to kick off a discussion about how Linux goes about finding files.

Before we start, it helps to think about commands as mini programs and most follow this structure: command -option(s) argument(s).

The which command has 1 option and the argument is the command you'd like to find.

Unlike many other commands, which does not have a double-dash --help option.

So which simply returns the location of the argument, assuming it's an executable, searching the what's called the PATH. And the PATH is a list of directories Linux will search when you enter a command. One caveat, on many distributions, which ignores the 60 or so shell builtins.

So why is which an important command? Well, which offers a quick answer if you are searching for command locations. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go, let's type man which, for two reasons, first because we don't have a double-dash-help option and second I wanted a throwback to those corny Manwhich commercials from the 1980s. ;)

WHICH(1) General Commands Manual WHICH(1) NAME which - locate a command SYNOPSIS which [-a] filename ... DESCRIPTION which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell. It does this by searching the PATH for executable files matching the names of the arguments. It does not follow symbolic links. OPTIONS -a print all matching pathnames of each argument (remaining lines trimmed)

Next, let's use multiple arguments, one for the shell builtin command type, and another executable like man.

$ which type man /usr/bin/man

Notice how the builtin type is ignored? An inconvenience you are now aware of.

A Final Tip

Okay now you know how to use which. And you know the syntax for commands, options and arguments.

One last tip about the which command. Yes, which has limited value, but offers an opening to the PATH variable, coming up next.

Okay, thanks for visiting today. I hope this was a fun introduction to the which command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

The best way to leave feedback and ask questions is on YouTube. Subscribe there and follow @factorpad on Twitter for new content reminders.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / which-command.html


linux which command
which command
multiple command locations
how to find commands in linux
which command syntax
linux which examples
which command summary
which command options
linux which tutorial
find command locations
unix which
locate commands in linux

A newly-updated free resource. Connect and refer a friend today.