whichcommand implemented as a Bourne shell script.
I just love scripts and finding new ways to use them. My contribution is a little script that is particularly nice because I can use it in combination with other Unix commands inside an alias. It is simple and powerful (one might even say elegant).
wh script is used to
locate the first or all occurrences (
-a option) of a
file in your shell's
command-search path. The other options let you display additional
information, such as file type (determined by the
file command when you
-f option) and long-directory
listing (specified with
-l option to
wh). Additionally, it can display the list of files
that match a substring (
-s option). I've also found
-d option helpful when I wanted to reference the
directory containing the located command. The name ``wh'' might
be considered shorthand for which, where, or what.
wh started life as a simple script to find a
file in my command-search path. I added features as the need
arose or I had a new idea. Then, I began looking for ways to use
wh in combination with other commands. First, I
vi inside an alias to
give me the ability to quickly start up an editing session for a
file in my bin
directory without specifying the path name to the file. Other
aliases soon followed. The C shell aliases that I use currently
alias fcd 'cd `wh -d \!*`;ls' alias fvi 'vi `wh \!*`' alias fcp 'echo copying `wh \!*` to current directory..;cp -i `wh \!*` .;' alias fpd 'pushd `wh -d \!*`;ls'
The ``f'' in ``fvi'' stands for find-the-file and ``vi'' it.
In retrospect, I could have named it ``wvi'' but I don't like the
way it looks. Consequently the names of all my other aliases using
wh also begin with a lower-case ``f.''
Editor's Note: Here are some sample command-line usage examples:
% wh Must specify at least one command name Usage: wh [-a] [-d] [-f] [-l] [-s] command-name... ( -a print all instances in path ) ( -d print directory path only ) ( -f print type of file ) ( -l print ls -l for file ) ( -s use case-sensitive substring match ) % wh date /bin/date % wh -a date /bin/date /usr/bin/date /usr/5bin/date % wh -d date /bin % wh -f date /bin/date: sparc pure dynamically linked executable % wh -l date -rwxr-xr-x 1 root 7456 Apr 15 1991 /bin/date % wh -s date /bin/date /usr/bin/date /usr/ucb/rdate /usr/5bin/date /usr/local/scripts/convert.date /usr/local/scripts/date_login /usr/local/scripts/dateorder /usr/local/scripts/dateorder-1 /usr/local/scripts/getdate /usr/local/scripts/update.yr.appt % wh -a -f date /bin/date: sparc pure dynamically linked executable /usr/bin/date: sparc pure dynamically linked executable /usr/5bin/date: sparc demand paged dynamically linked executable % wh -a -l date -rwxr-xr-x 1 root 7456 Apr 15 1991 /bin/date -rwxr-xr-x 1 root 7456 Apr 15 1991 /usr/bin/date -rwxr-xr-x 1 root 16384 Apr 15 1991 /usr/5bin/date % wh -d -f date /bin: symbolic link to usr/bin % 
Last Modified: Wednesday, 23-Aug-95 16:56:02 PDT