Blog

03/06/2019

Linux / Unix: which Command Examples To Find Out A Program File



I am a new Linux and Unix-like system user. How do I find out gcc compiler path on Linux? How can I locate a program file in path on Linux or Unix-like systems?

The which command shows the full path for each of its given arguments. This command searchers for an executable or script in the user’s PATH.[donotprint]

which command details
DescriptionLocate a program file
Category
Difficulty
Root privilegesNo
Estimated completion time5m
[/donotprint] I often use which command to find the executable file associated with a given command name.

Purpose

Display the full path of shell commands.

Syntax

The basic syntax is as follows:

which command

OR

which [option] command

OR

which prgoram

which command examples

To find the executable file associated with a command name of date:
$ which date

To find the executable file associated with a command name of httpd, top, and ls, type:
$ which httpd top ls

How can I list all instances of executables found?

Pass the -a option to the which command as follows:
$ which -a command
$ which -a ls

Sample outputs:

which command options

From the which(1) command man page:

OptionMeaning
--skip-dotSkip directories in PATH that start with a dot.
--skip-tildeSkip directories in PATH that start with a tilde.
--show-dotDon’t expand a dot to current directory in output.
--show-tildeOutput a tilde for HOME directory for non-root.
--tty-onlyStop processing options on the right if not on tty.
-aPrint all matches in PATH, not just the first
-iRead list of aliases from stdin.
--skip-aliasIgnore option -i; don’t read stdin.
--read-functionsRead shell functions from stdin.
--skip-functionsIgnore option –read-functions; don’t read stdin.

On Linux bash defined which as function as follows:

which ()
{
   (alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@
}
export -f which

tcsh / csh user can create an alias as follows:

alias which 'alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

In the Korn shell, you can use the whence command for the same purpose:
$ whence ls
$ whence date httpd ls

See also
  • which(1) Linux/Unix command man page
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Package Managerapkapt
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationidgroupslastlastcommlognameuserswwhowhoamilid/libuser-lidmembers

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...