ECN No Name Newsletter: June, 1993

The ECN No Name Newsletter is no longer being published. This is an archived issue.

[previous article] [next article]

FINDIT Like Magic

NO NAME NEWSLETTER -- June 1993

Marian Delp


A few weeks ago I was looking for a pattern that I knew was somewhere in one branch of my home directory. After searching I was able to locate what I was looking for. It took awhile using the grep command, but I finally pinpointed the elusive item. It was two directories down from where I started in a small forgotten file. Very easy to find, once I knew where to look!

Later it became obvious that I would need to repeat this search (for different patterns) numerous times. Rather than repeat the method first used (file by file, or directory by directory), I went to Kent De La Croix and asked if he knew of some UNIX magic to solve my problem. After a few moments he wrote the following code:


          find . -type f -exec grep -i $1 {} \; -print

I placed this code in a file I called findit in my bin directory that I keep in my HOME directory. Because I use the csh, I needed to issue the rehash command to make it possible for my new command to be located when used. Now anytime there is need to locate "something", it can do it by typing "findit word" .

This handy tool is made of the following parts:

    find        UNIX command for recursively descending
                directory hierarcy while doing a search
                current directory is search starting point

    -type f    search files; not directory filename listings

    -exec      execute to semicolon

    grep       UNIX searching program

    -i         ignores upper and lower case (capitalization)

    $1         holder for argument "word" from command line

    {}         holder for file name currently being matched

     ;         completion marker for this search element

    -print     print to the screen the full pathname for
               all files where the "word" is located

If I was to use this tool to search for the word "Marian", my screen would look like the following:

     mayflower.ecn.purdue.edu% findit marian

Edited by Marian Delp

./Newsletters/93.june/working.copy

for marian: stained glass, watch, ruby pendant

./Xmas/94.wishes/big.gifts

NOTE: When the searching tool locates the "word", the text line in which it appears is placed first on the screen with the pathname printed on the second line. In the above example, two occurrences of the "word" were located.


webmaster@ecn.purdue.edu
Last modified: Saturday, 13-Sep-97 00:23:14 EST

[HTML Check] HTML