[PLUG] How find command works ?

Arindam arindam.mukerjee at gmail.com
Thu Jan 4 12:08:37 IST 2007


On 1/4/07, Amol Dongare <amolmdongare at gmail.com> wrote:
> Hi All,
>
>  I wanted to know how find command works ? how it searches ?

Do you want to know the syntax or how it is implemented?

>  Can anybody help me out ?

Guessing that you want to know how it would typically be implemented:

It would involve calling opendir on the base search directory passed
to the find command. It would recurse through the directory entries,
avoid . and .. . It will stat (possibly using lstat system call) each
directory entry and try to find a match with the find criteria you
specified - that could be name or some other attribute.

If you specified some command to execute on each match using -exec,
then it would do a fork-exec and execute the command on the matching
entry.

It would print the matching entry names to the standard output and go
on till it has recursed through all the sub-directories of the base
search directory, or recursed upto maxdepth nested subdirectories
under each directory (if you set max-depth on the command line).

The above is pretty simplistic but that's the scheme followed more or less.

A good way to guess how these commands are implemented is:

strace find mydir -name "*"

On non-linux systems, like Sun Solaris, you can try this:

truss find mydir -name "*"


>  Thanks in Advance !!!
>
> --
> Regards,
>
> Amol M Dongare
> --
>

-- 
Against stupidity, the very Gods themselves contend in vain --
Friedrich Schiller




More information about the Plug-mail mailing list