{

Which command in Linux|Unix with examples


Which command is a frequently used command to find the location of executable files in linux/unix

During application development, I can use this command to locate the tomcat location for logs finding.

In this tutorial, We are going to learn about which command and syntax and usage examples

Syntax

which options [string array]

This command accepts,

  • options
  • -a displays all pathnames of given input string
  • string is an input filename which compared and checked against PATH folder paths

Returns the list of path of filenames

Before returning thsi command, It returns following exit codes 0 - given filename matched with executable paths 1 - filename(s) are not executable found 2 - It returns if an option is invalid argument,

print all matching pathnames of each argument which command search for the executable files configured to environment variable PATH

PATH is an environment variable that that contains all the directories pointed to executable files

First print the PATH environment values using echo command

$:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

For example, to check where is git command is installed on linux machine

$:~$ which git
/usr/bin/git

git command executable found in /usr/bin folder

which command options

Please see below example with and without options

-a option prints all matched pathnames for a given input string

$:~$ which git
/usr/bin/git

$:~$ which -a git
/usr/bin/git
/bin/git

$:~$ 

print the which command documentation using info which command

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 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 executable
       2      if an invalid option is specified
       

Sometimes which command does not works or not return anything on console

Then you can use **whereis command ** same like which syntax.

Conclusion

This article shows how to clear the terminal commands from different Operating Systems.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.