Linux Commands Examples

A great documentation place for Linux commands

which

locate a command

Synopsis

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!

examples

0
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
0
source

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:

#!/bin/bash
[[ $# -gt 0 ]] || { echo "Usage: $0 <filename> [pathspec]" >&2 ; exit 1 ; }
if [[ $# -gt 1 ]] ; then
        P="$2"
else
        P="$PATH"
fi

IFS=:
for DIR in $P ; do
        if [[ -e "$DIR/$1" ]] ; then
                echo "$DIR/$1"
                exit 0
        fi
done

Example:

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

$ ./search.sh ls
/Users/danielbeck/bin/ls

$ ./search.sh pwd
/bin/pwd

$ ./search.sh ls /bin
/bin/ls

$ ./search.sh ls /usr/bin:/bin
/bin/ls

0
source

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

/usr/local/bin/execfile

/usr/bin/execfile

0
source

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

0
source

Can I use `which` command for partial match?

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

#!/bin/sh
IFS=:
for i in $PATH; do
    for j in "$i"/$1; do
        [ -f "$j" ] && [ -x "$j" ] && printf '%s\n' "$j"
    done
done

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

$ echo $PATH
/usr/local/bin:/usr/bin:/bin
$ ./whichglob grep*
/usr/bin/grepdiff
/usr/bin/grep-excuses
/usr/bin/grepjar
/bin/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.

0
source

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

On a certain system I get this output:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

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.

description

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.

options

-a

print all matching pathnames of each argument

exit status

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

How can this site be more helpful to YOU ?


give  feedback