How to Use the which Command in Linux

by Marlon Altenwerth
12 minutes read

How to Use the which Command in Linux


Which Command In Linux

Linux offers you persistent manipulate over how you occupational, from customization to unit management. One handy contraption that can gain your fiasco smoother is the which command. It helps you promptly detect the exact void of an executable documents by scouring via the magazines gambled out in your $PATH variable. In this guide, we’ll define how the which command jobs, dissect its phrase texture and varieties, and go via handy instances to help you gain gain service of of it entirely.

Giving the which Command Syntax and Massaging

The which command in Linux helps detect the executable documents for a granted command. It scours magazines gambled out in the PATH undercurrent variable. This command reflects which documents runs once you execute a command. It reverts the subsequent upshot:

  • 0 once unanimously delineated commands are spotted and executable.
  • 1 if one or much more commands are lacking out on or not executable.
  • 2 if an discontinuity replacement is grossed gain gain service of of of.

The basic phrase texture to gain gain service of of which command in Linux is:

which [file1] [file2] ...

Replace [file1] and [file2] with the names of the executables you want to detect.

Why Ought to We Application which Command

There are the majority of factors to gain gain service of of the which command in Linux. For example, it helps us acknowledge if a command is an actual executable, an alias, or a symbolic internet linkage. In addition, it displays the exact void of an executable documents. It correspondingly enables us to acknowledge lacking out on or conflicting commands in the PATH variable. In addition, using which command makes sure the rectify model runs once multifarious models exist.

How to Application Which Command in Linux

Let’s dissect some instances to go to how which command jobs in real gain gain service of of pills:

Instance 1: Position the Training course of an Executable

We can gain gain service of of the which command to acknowledge whereby a command’s executable documents is preserved:

which grep
Position Training course Of Executable
How to Use the which Command in Linux 29

It reverts the complete path, such as “/usr/bin/grep”, reflecting the exact void of the executable.

Instance 2: Position Training courses of Multiple Commands

We can specify multifarious commands unanimously at once with the which command to discover their synchronizing paths:

which awk sed cat touch

This command fetches the paths of awk, sed and cat commands, specifically:

Position Training course Of Multiple Commands
How to Use the which Command in Linux 30

Instance 3: Show unanimously Instances of a Command

By default, which reverts uncompanionable the first match spotted in the PATH. Yet, we can execute the which command with the -a replacement to detect unanimously pills of a command:

which -a cat

This command lists unanimously units whereby cat is spotted in PATH:

Show Unanimously Instances
How to Use the which Command in Linux 31

You can confirm their niceties, by applying the ls command with the -lh replacement. This reflects if both records are real executables or simulates in polymorphous units:

ls -lh /usr/bin/cat && ls -lh /bin/cat
Reflect Executables
How to Use the which Command in Linux 32

The screenshot above displays 2 homogenous commands in polymorphous units, both 35KB in dimension and executable.

Instance 4: Hearken if a Command Exists

We can gain gain service of of the which command to discover the comportment of a picky command:

which cats
Missing out on Commands
How to Use the which Command in Linux 33

If the command does not exist, there is no upshot, as authenticated in the screenshot above, and the exit eminence is non-zero.

Instance 5: Recognize Symbolic Institutions

A unit could have multifarious models of unchanged regimen, but some instances could be symbolic linkages instead of actual binary records. To detect the symbolic linkages, just sprinted the which command with -a replacement:

which -a crontab
Hearken Symbolic Institutions
How to Use the which Command in Linux 34

Next, discover the documents niceties with the ls command:

ls -lh /usr/bin/crontab && ls -lh /bin/crontab
Hearken Record Veracities
How to Use the which Command in Linux 35

If the upshot incorporates ->, it supposes the documents is a symbolic internet linkage pointing to an additional void.

Instance 6: Run away out Rind Prefabricated-in Commands

The which command uncompanionable displays exterior executables and does not show pill taken place-in commands. It helps us acknowledge whereby a regimen is installed on the unit. For example, in the subsequent command which uncompanionable reverts a path for ls granted that read is a taken place-in pill command:

which ls read
Run away out Rind Prefabricated In Commands
How to Use the which Command in Linux 36

Which Command Hindrances

The which command owns multifarious borders that gain it less trusty in exact situations. For example, it uncompanionable reflects exterior executables and does not detect taken place-in commands advice cd or read. Better, it cannot separate in between binary and symbolic linkages. The command purely scours magazines gambled out in the $PATH variable, anticipating it cannot detect regimens preserved in other places. Just as, it didn’t lend niceties advice documents kind, consents, or model. Better, if a command doesn’t exist, it does not brandish an misstep blog post but just reverts nothing.

To leapt over these borders, you can opt for an unanimously natural command advice whereis, type, or ls.

Whereby Vs. Which Command

The where and which commands both help position records but serve polymorphous duties. The where command is largely grossed gain gain service of of of in Windows and some Unix-advice equipments. It locates both records and magazines and lists unanimously matches in the unit’s PATH.

The which command is picky to Unix-advice equipments. It scours uncompanionable for executable records in magazines gambled out in the $PATH variable. By default, it reverts the first match, but using which -a lists unanimously matches.

For example, sprinting where node command on Windows displays unanimously units whereby node is spotted. On Linux, which gcc reverts the first path whereby the gcc compiler is located.

While the which command deals multifarious amenities, it still shortages some capacities. Yet, this can be leapt over by using an unanimously natural command, as claimed earlier. In addition, the which command uncompanionable uncovers executable records in your $PATH. If your manuscript or regimen isn’t spotted, it could be lacking out on execute consents. Therefore, gain sure your manuscript is in a brochure gambled out in $PATH and owns execute consents.

Related Posts