Linux Unix which command

Introduction

In Linux and Unix Operating Systems, commands are executable files. The commands may be specific with user environment. We can set the absolute path of specific command as per user in environment. To get the absolute path of command. In that case we will use which command.

The which command is External command. (Learn more about ‘What is Internal and External command in Linux/Unix‘ and ‘type command‘)

To find the absolute path of command in Linux/Unix system, we use which command. Given below is the Syntax.

Syntax:

which command-name

# OR

which executable-file-name

Note: The echo $PATH command will show the directory path. The which command, locate the command from these directories.

For example:

root@tuxworld:/# echo $PATH
/usr/local/rvm/gems/ruby-2.1.0/bin:/usr/local/rvm/gems/ruby-2.1.0@global/bin:/usr/local/rvm/rubies/ruby-2.1.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/rvm/bin
root@tuxworld:/# 

Example : In this example,we will find the absolute path of useradd command.
The output from which command is showing the absolute path.

root@tuxworld:/# which useradd
/usr/sbin/useradd
root@tuxworld:/#

We can use multiple arguments in which command. It will help us to find path of two or more executable files and commands. By writing single line of command on terminal will give you the result.

Syntax:

which command-1 command-2 command-N

Example: In this example,we are finding the absolute path of ls,chown,chgrp and usermod command.

root@tuxworld:/# which ls chown chgrp usermod
/bin/ls
/bin/chown
/bin/chgrp
/usr/sbin/usermod
root@tuxworld:/# 

To print all matching pathnames of each argument,we use -a option with arguments i.e command name.

Syntax:

which -a argument

Example: In this example, we are searching absolute path of echo command

root@tuxworld:/# which -a echo
/usr/sbin/echo
/bin/echo
root@tuxworld:/#

Read Some More Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.