How to find directory of some command?

LinuxShellCommand Line

Linux Problem Overview


I know that when you are on shell, the only commands that can be used are the ones that can be found on some directory set on PATH. Even I don't know how to see what dirs are on my PATH variable (and this is another good question that could be answered), what I'd like to know is:

I come to shell and write:

$ lshw

I want to know a command on shell that can tell me WHERE this command is located. In other words, where this "executable file" is located?

Something like:

$ location lshw
/usr/bin

Linux Solutions


Solution 1 - Linux

If you're using Bash or zsh, use this:

type -a lshw

This will show whether the target is a builtin, a function, an alias or an external executable. If the latter, it will show each place it appears in your PATH.

bash$ type -a lshw
lshw is /usr/bin/lshw
bash$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls
bash$ zsh
zsh% type -a which
which is a shell builtin
which is /usr/bin/which

In Bash, for functions type -a will also display the function definition. You can use declare -f functionname to do the same thing (you have to use that for zsh, since type -a doesn't).

Solution 2 - Linux

Like this:

which lshw

To see all of the commands that match in your path:

which -a lshw 

Solution 3 - Linux

PATH is an environment variable, and can be displayed with the echo command:

echo $PATH

It's a list of paths separated by the colon character ':'

The which command tells you which file gets executed when you run a command:

which lshw

sometimes what you get is a path to a symlink; if you want to trace that link to where the actual executable lives, you can use readlink and feed it the output of which:

readlink -f $(which lshw)

The -f parameter instructs readlink to keep following the symlink recursively.

Here's an example from my machine:

$ which firefox
/usr/bin/firefox

$ readlink -f $(which firefox)
/usr/lib/firefox-3.6.3/firefox.sh

Solution 4 - Linux

~$ echo $PATH
/home/jack/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
~$ whereis lshw
lshw: /usr/bin/lshw /usr/share/man/man1/lshw.1.gz

Solution 5 - Linux

In the TENEX C Shell, tcsh, one can list a command's location(s), or if it is a built-in command, using the where command e.g.:

tcsh% where python
/usr/local/bin/python
/usr/bin/python

tcsh% where cd
cd is a shell built-in
/usr/bin/cd

Solution 6 - Linux

An alternative to type -a is command -V

Since most of the times I am interested in the first result only, I also pipe from head. This way the screen will not flood with code in case of a bash function.

command -V lshw | head -n1

Solution 7 - Linux

The Korn shell, ksh, offers the whence built-in, which identifies other shell built-ins, macros, etc. The which command is more portable, however.

Solution 8 - Linux

TLDR Answer

Use: whereis -b lshw.

Explanation

Use the whereis command. From the man page:

>whereis - locate the binary, source, and manual page files for a command

Commonly-Used Switches

In addition, you can specify what you're looking for:

  • whereis -b packagename : Source for location of binaries.
  • whereis -m packagename : Source for location of manuals.
  • whereis -s packagename : Source for location of source code.

In your case, since you're looking for the binary, you'll want: whereis -b lshw.

There are other switches with this command, check them out at the man page. If there is no file associated with a packagename, you'll see a blank line.

Examples

Here's some real world use:

holdoffhunger@tower:~$ whereis grep
grep: /bin/grep /usr/share/man/man1/grep.1.gz /usr/share/man/man1/grep.1posix.gz

holdoffhunger@tower:~$ whereis -m grep
grep: /usr/share/man/man1/grep.1.gz /usr/share/man/man1/grep.1posix.gz

holdoffhunger@tower:~$ whereis -s grep
grep:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabriel L. OliveiraView Question on Stackoverflow
Solution 1 - LinuxDennis WilliamsonView Answer on Stackoverflow
Solution 2 - LinuxIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 3 - LinuxhasenView Answer on Stackoverflow
Solution 4 - LinuxJackView Answer on Stackoverflow
Solution 5 - LinuxPierzView Answer on Stackoverflow
Solution 6 - LinuxMarinos AnView Answer on Stackoverflow
Solution 7 - Linuxmpez0View Answer on Stackoverflow
Solution 8 - LinuxHoldOffHungerView Answer on Stackoverflow