WebMasterCampus
WEB DEVELOPER Resources

Linux which Command

Learn Linux which Command with examples


Linux which Command

In Linux, we can use “which” command is used to locate the executable files or location of a program from the file system. It displays the path where the specified file or command is stored.

The command searches for the executable specified as an argument in the directories listed in the PATH environment variable.

which Command Syntax

>> which <program name>

which Command Example

To find the full path of the ps command

>> which ps

/usr/bin/ps

which Command Multiple Program Names

To display multiple paths of executable files by providing multiple arguments at a time in a single command.

To pass the multiple arguments in which command, separate them by a space.

>> which which cat mkdir

/usr/bin/cat
/usr/bin/mkdir

which -a Command

To print all matches, use the -a option.

>> which -a sleep

/usr/bin/sleep
/bin/sleep

Normally, one of the executables is only a symlink to the other one.

which Command Exit Status

The Linux user has an exit status, or it is executed by the shell script.

  • If the exit status is 0, it means the command is successfully executed without any error.
  • If the exit status is the non-zero number (i.e., 1 to 255), it means the command had an error and was a failure.
Command Description
0 If the exit status is 0; it means all arguments are found and executable.
1 If the exit status is 1; it means one or more arguments are non-existent or non-executable.
2 If the exit status is 2; it means an invalid option is specified.

which Command in Linux (Documentation)

~$ man which

NAME
       which - locate a command

SYNOPSIS
       which [-a] filename ...

DESCRIPTION
       which returns the pathnames of the files (or links) which would be ex‐
       ecuted 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  ar‐
       guments. It does not canonicalize path names.

OPTIONS
       -a     print all matching pathnames of each argument

EXIT STATUS
       0      if all specified commands are found and executable

       1      if  one  or  more specified commands is nonexistent or not exe‐
              cutable

       2      if an invalid option is specified
Created with love and passion.