Whence and Whenceman

On the UNIX command line, the `which` command is great, it tells you where a command is in the system.  However, if your system has two versions of the `ls` command, it will only tell you which `ls` command you are going to use when tap it in and press enter.  To find all copies of any command, we need something Liam called `whence` (I inherited Liam’s bashrc file when working at Sun, and this little gem was right inside it).  The .bashrc function for `whence` looks something like this:

# search for all instances of an executable in $PATH
function whence {
for i in `echo $PATH | sed "s/:/ /g"`
do
/usr/bin/ls $i/$@ 2>/dev/null
done
}

It’s nice and straighforward, nothing complicated about it at all.  In fact, to let everyone on your system use it, you could just stick “#!/bin/sh” at the top and stick in a file in /usr/bin !

Today, I wanted to find a man page, the sysidcfg man page to be precise.  Instead of doing the usual trick (`find / | grep sysidcfg”), I thought I’d modify `whence` to look for it for me, and seeing as it’s no longer `whence`, I called it `whenceman`:

# search for all instances of a man page in $MANPATH
function whenceman {
for i in `echo $MANPATH | sed "s/:/ /g"`
do
/usr/bin/find $i/ 2>/dev/null | grep -i $@
done
}

As you can see, it’s very similar, and you could do the same thing by putting it in /usr/bin so everyone could use it.

This entry was posted in Bash/Shell, Linux, Mac OS, OpenSolaris and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>