September 20, 2025
Linux Command Line

Linux Command Line

The ‘which’ command is a simple yet powerful command line utility in Linux that allows users to locate the binary files associated with a specific command. It is particularly useful for finding the location of executables, scripts, and other system commands. In this article, we will explore the uses of the ‘which’ command, and demonstrate how to use it with practical examples.

Understanding the ‘which’ Command:

The ‘which’ command is used to locate the executable file associated with a command that is entered in the terminal. It searches for the command in the directories specified in the PATH environment variable and returns the path of the executable file. This can be useful when you have multiple versions of a command installed on your system and you need to know which one is being used.

Using the ‘which’ Command:

The ‘which’ command is used by simply typing “which” followed by the command you want to locate. For example, to find the location of the ‘ls’ command, you would enter “which ls” in the terminal. The command will then return the path of the executable file associated with the ‘ls’ command.

Examples:

  1. To find the location of the ‘ls‘ command: $ which ls /bin/ls
  2. To find the location of the ‘grep‘ command: $ which grep /bin/grep
  3. To find the location of the ‘python’ command: $ which python /usr/bin/python
  4. To find the location of the ‘nano‘ command: $ which nano /usr/bin/nano

Using ‘which’ with Aliases:

Another useful feature of the ‘which’ command is that it can also be used to locate the source of an alias. For example, if you have an alias called ‘ll’ that is set to run the ‘ls -l’ command, you can use ‘which’ to find the source of the alias.

$ alias ll=’ls -l’

which ll

alias ll=’ls -l’

The ‘which’ command is a simple yet powerful tool for locating the executable file associated with a command in Linux. It can be used to quickly identify the source of a command, whether it is a binary file or an alias, and can be particularly useful when working with multiple versions of a command or when troubleshooting issues with a specific command. Additionally, it can be a useful tool for system administrators to quickly locate and access the necessary executables for managing a Linux system. It’s simple yet powerful command, you can use it in different scenarios, for example, you can use it in shell script to locate the path of a specific command or in a Makefile to locate the path of a specific tool. It is a must-know command for anyone working with Linux.

See our list of 75 Linux commands you should know about.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Share via
Copy link
Powered by Social Snap