DEV Community

Cover image for How the which Command works on Linux
Johnny Simpson
Johnny Simpson

Posted on • Edited on • Originally published at fjolt.com

How the which Command works on Linux

When we are running servers, or even our local computer, different applications may install the same piece of software multiple times. For example, it is not uncommon to accidentally have two versions of Node.JS installed on a server or computer.

In the example where we have multiple versions of Node.JS, it can be confusing which versions are running, or which will be used when we run the node command in a terminal window.

If we want to know the origins of a command, we can use the which command to find where it is installed. The which command has the following syntax, [x], [y] and [z] are what we want to check:

which [x] [y] [z]
Enter fullscreen mode Exit fullscreen mode

How to use the which command on Linux or Mac

Lets use our Node.JS example to start with. If we want to know which Node.JS is being used, we can simply type the following:

which node
Enter fullscreen mode Exit fullscreen mode

This will then return something like this:

/root/.nvm/versions/node/v14.15.1/bin/node
Enter fullscreen mode Exit fullscreen mode

Checking multiple commands with the which command on Linux or Mac

If we want to check the location of multiple commands on Linux or Mac, we can use the usual which syntax, but just separate each item we want to check with a space.

For example, the below text checks both node, and postfix:

which node postfix
Enter fullscreen mode Exit fullscreen mode

And for me, it returns this:

/root/.nvm/versions/node/v14.15.1/bin/node
/usr/sbin/postfix
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
chabala profile image
Greg Chabala

That's how to use 'which'. How 'which' works is a much more interesting topic.

Collapse
 
smpnjn profile image
Johnny Simpson

I would argue it's probably only sort of interesting

Collapse
 
chabala profile image
Greg Chabala

My point is your title is misleading. How something works and how to use it are different topics. If you titled an article 'How a TV works', and proceeded to explain how to turn it on and change the channel and volume, I'd be similarly disappointed.

Thread Thread
 
smpnjn profile image
Johnny Simpson • Edited

I think the issue is that fundamentally the verb "work" in english is ambiguous.

Thread Thread
 
chabala profile image
Greg Chabala

If you think it's ambiguous, that's more reason to use a better word. I don't think works is ambiguous, just incorrect.