Open In App
Related Articles

Which Command in Linux with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

which command in Linux is a command which is used to locate the executable file associated with the given command by searching it in the path environment variable. It has 3 return statuses as follows:

  • 0 : If all specified commands are found and executable.
  • 1 : If one or more specified commands is nonexistent or not executable.
  • 2 : If an invalid option is specified.

Syntax of `Which` Command in Linux

The basic syntax of which command is

which [filename1] [filename2] ...

Basic Usage

It is basically used to find the location of the executable file associated with the command.

For Example: To find the location of the “cd” command, we use the following command 

which cd

which cd

The output will display the full path to the “cd” executable file, such as “/bin/cd”

Some commonly used Options and Examples of the `which` command in Linux.

`-a` can be used to display all the occurrences of the file.

Simple `which` command will display the path of the first occurrence, but if we want ot display all the occurrences of the file we can use `-a` option.

For Example: If we want to display all the occurrences of the `python` file.

which -a python
which -a python

which -a python

Checking Command Aliases

We can use which command with `-a` option to check whether the command is an alias or not.

For Example: If we want to check whether the command `ll` is an alias or not.

which -a ll
which -a ll

which -a ll

`-v` can be used to display more verbose output.

We can use `-v` option with `which` command to display more verbose and additional information.

For Example: If want o display more verbose and additional information of `echo` command.

which -v echo

which -v echo

To display help information of the `which` command

info which: It displays help information

info which
info which

info which

Conclusion

In this article we have discussed about `which` command that is mainly used to locate the executable file associated with the given command. We have discussed Options available and there usage. One can easily understand the working of `which` command by going through this article.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 09 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials