UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 16.9 An Alias to List Recently Changed Files Chapter 16
Where Did I Put That?
Next: 16.11 Showing Hidden Files with ls -A and -a
 

16.10 findcmd: Find a Command in Your Search Path

UNIX has utilities like whereis (50.5) and which (50.8) to look for a command on the system. But whereis doesn't look in your shell's search path, so it may not find shell scripts in local system directories or your bin directory (4.2). And to use which, you have to know the exact name of the command, because which only shows the first command with that name in your path.

If you're like me, you can't always remember the name of the command you're looking for. "Wasn't it called reference or refer or something like that?" The findcmd script saves me a lot of guessing. It shows all command names, in all directories in my search path, that contain some string. So, I'll look for command names that have "ref" in them:

% findcmd ref
/home/jerry/.bin/zrefile
/usr/bin/X11/xrefresh
/usr/local/bin/grefer
/bin/cxref
/bin/refer
/usr/bin/cxref
/usr/bin/refer
./preferences

findcmd
After a couple of tries, I usually find the command I want. The findcmd script is on the CD-ROM.

First, the script edits a copy of your PATH (6.4) to change any current directory entry to a dot (:.:). Next, a colon (:) in the IFS (35.21) variable lets the shell split the PATH at the colons; a for loop (44.16) steps through each directory in the PATH and runs ls -l to find matching files. Finally, a sed (34.24) script reads through the output of all the ls commands in the loop, editing and printing matching lines (executable files with the program name we want).

- JP


Previous: 16.9 An Alias to List Recently Changed Files UNIX Power ToolsNext: 16.11 Showing Hidden Files with ls -A and -a
16.9 An Alias to List Recently Changed Files Book Index16.11 Showing Hidden Files with ls -A and -a

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System