views:

96

answers:

5

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

Anyone?

+16  A: 

Like this:

which lshw
Ignacio Vazquez-Abrams
also which -a lshw to see all of the commands that match in your path.
qor72
+3  A: 
~$ 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
Jack
+3  A: 

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
hasen j
A: 

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

mpez0
In ksh, `whence -a` is similar to Bash's `type -a`.
Dennis Williamson
+2  A: 

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

Dennis Williamson
Thank everybody who answered, but this answer was the most exciting! Thank you! I was thinking that I'm crazy, because I defined (a long time ago) a way to do this: 'update', and this has been doing 'apt-get update; apt-get dist-upgrade' for me. But for now, I was trying to find some update.sh file somewhere, and I couldn't find it. That's why I started this question. But now, using 'type -a update' I found that this was just an alias defined on my .bashrc located on my ~home. Really thank you.
Gabriel L. Oliveira
@Gabriel: If you're not familiar with `locate` it can help find files. It uses a database that's maintained by `updatedb` which is run from a cron job. Since `locate` searches a database rather than the whole filesystem it's much faster than `find` (which could be used as a last resort).
Dennis Williamson
Thank you. I'll study this tool, and see how updatedb is scheduled to run on Ubuntu's cronjob.
Gabriel L. Oliveira