The which Command
The which command 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 variable for executable files matching the names of the arguments.
pbmac@ubuntu $ echo $PATH /home/pbmac/.rbenv/plugins/ruby-build/bin:/home/pbmac/.rbenv/shims:/home/pbmac/.rbenv/bin: /home/pbmac/scripts:/home/pbmac/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin: /usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/bin: /var/local/Android-SDK/Android-sdk/platform-tools:/var/local/Android-SDK/tools: pbmac@ubuntu $ which ping /usr/bin/ping pbmac@ubuntu $ which netcat uptime /usr/bin/netcat /usr/bin/uptime pbmac@ubuntu $ which -a touch /usr/bin/touch /bin/touch /bin/touch pbmac@ubuntu $
The PATH is the directories that Linux will look in to find an executable file when you attempt to issue a command, either from the command line, or selecting an icon on a GUI based system. in this example, Linux will sequentially search through each directory looking for the command, and will execute the first file that matches the user's input.
which can accept a single argument, such as ping, or multiple arguments as shown with netcat and uptime; and if you use the -a option, it will show ALL of the files with the given name, not just the first one it finds. In the example it found two files named touch and lists the path to both files.
The use of the -a option is a great tool to make sure you are executing the file you think you are executing. If there is another file named the same, and it is in your PATH before the file you want to execute, the first one gets executed. This can cause you to see output that is not what you expected, or different from what you expected.