In Linux and Unix 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. If we require the information, to get the absolute path of command. In that case we will use which
command.
Table of Contents
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:/#
Print multiple command’s absolute path
We can use multiple arguments in which
command. Hence,we can show two or more executable files absolute path on terminal.
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:/#
print all matching pathnames of each argument
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:/#