Open In App

How to Display Path of an Executable File in Linux | Which Command

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

which command in Linux is a command that 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] ...

How to Display Path of an Executable File in Linux

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”

Pratical Examples and Options of the `which` Command in Linux

1. Using `-a` 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

2. 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

3. Using `-v` 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

4. Display help information of the `which` command

info which: It displays help information

info which

info which

info which

Frequently Asked Questions

1. How to use the ‘which’ command in Linux to find the path of an executable?

Use the which command followed by the command name.

For example, to find the full path of the ls command, type:

which ls

2. How can I check if a command exists in Linux?

Use the which command. If it returns a path, the command exists. If it doesn’t, the command is not found.

3. How can I find out where a command is installed in Linux?

Use the which command to find the path to the command, and then use the ls -l command to view information about the executable file.

4. How do I use which command with options in Linux?

The which command has a few options, such as:

  • -a: Lists all instances of a command found in the path.
  • -s: Silent mode; doesn’t output anything if the command is not found.

5. How does the which command differ from the whereis command?

  • which finds the first executable file matching a command name in the directories listed in your PATH environment variable.
  • whereis locates a command’s binary, source code, and manual pages, searching in standard system directories.

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads