Linux Commands Examples

A great documentation place for Linux commands


locate a command


which [-a] filename ...

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!


Note that "which" *doesn't* tell you what command would be executed in contexts where the shell has builtins or
functions of the same name.  sh/ksh/bash users should use the "type" command instead of "which" (which is mostly an obsolete holdover from csh).
example added by an anonymous user

Linux: find first result from specified search path

You could just use a script that simply tests (-e) for existence of a file and stops when the first one has been found:

[[ $# -gt 0 ]] || { echo "Usage: $0 <filename> [pathspec]" >&2 ; exit 1 ; }
if [[ $# -gt 1 ]] ; then

for DIR in $P ; do
        if [[ -e "$DIR/$1" ]] ; then
                echo "$DIR/$1"
                exit 0


$ ./ 
Usage: ./ <filename> [pathspec]

$ ./ ls

$ ./ pwd

$ ./ ls /bin

$ ./ ls /usr/bin:/bin

(Unix/Linux) How to execute different commands with same name?



'which' equivalent in Windows

It's WHERE to find any files in your path.

On XP/2003 and earlier, you need to add it from the Resource Kit, but it a standard command in Vista onwards.


Can I use `which` command for partial match?

Here is a quick and dirty sample script to do what you want in practice:

for i in $PATH; do
    for j in "$i"/$1; do
        [ -f "$j" ] && [ -x "$j" ] && printf '%s\n' "$j"

Save this as e.g. whichglob and make it executable. Sample run:

$ echo $PATH
$ ./whichglob grep*

Actually all the functionality in which (-a, exit statuses, multiple file match inputs) can be easily added in this shell script context as well, but I leave that as an exercise for the reader.


How to change the path that comes with the output of which command?

On a certain system I get this output:

$ echo $PATH

This means that upon executing the command

$ foo

the shell will look for executable files in this order:

  1. /usr/local/bin/foo
  2. /usr/bin/foo
  3. /bin/foo
  4. /usr/local/games/foo
  5. /usr/games/foo

and run the first one encountered.

which foo would return the path of the first of these matches, or nothing if an executable file named foo does not exist in the PATH directories (note the which -a switch which will return all matches in order, not just the first one).

You could e.g. reorder the directories in the PATH variable to change the lookup order, but that is probably not the solution to your "real" question. If you have an executable file in a higher priority directory "shadowing" the wanted one, you could either move the first one away, or simply execute the lower priority one with its full path.

I recognize mex as part of the Matlab installation. Trying to guess what you want to do, perhaps you could temporarily modify the PATH for a single running process as such:

$ PATH=/home/user/myownexecs:$PATH matlab

where /home/user/myownexecs/mex is the mex executable you want to give precedence. This will temporarily modify the PATH variable for the matlab process, but not interfere with the system in general in a lasting way.

You should clarify your question to ask what you really want to do to get more fitting answers, though.


which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell. It does this by searching the PATH for executable files matching the names of the arguments. It does not follow symbolic links.



print all matching pathnames of each argument

exit status


if all specified commands are found and executable


if one or more specified commands is nonexistent or not executable


if an invalid option is specified

How can this site be more helpful to YOU ?

give  feedback