Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How does find() build its tree.

by Ras (Acolyte)
on Jan 28, 2002 at 20:28 UTC ( [id://142096]=perlquestion: print w/replies, xml ) Need Help??

Ras has asked for the wisdom of the Perl Monks concerning the following question:

Correct me if i'm wrong but the find command builds a
tree to traverse down in a dir. How does Find build
that tree of files? Does it build it depending on
the time stamp or what? I hope this question is not
confussing. Thanks in advance.

Replies are listed 'Best First'.
Re: How does find() build its tree.
by japhy (Canon) on Jan 28, 2002 at 20:47 UTC
    It uses the directory entries in the order that the system call readdir(3C) returns them in. This is related to their device number and inode, I believe. It's some particular order, although I'm not sure what order it is.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Is there any way to sort what find returns by the time
      stamp?
        File::Find just retrieves all of the filenames and calls your "wanted" function once on each name. Anything more sophisticated has to be implemented by you in terms of this "wanted" function.

        For example, the following script will perform a find on /tmp, then print all of the file names in order from most recent to oldest:

        #!/usr/bin/perl -w use strict; use File::Find; my @files; sub wanted { push @files, [ $File::Find::name, -M $_ ]; } find(\&wanted, "/tmp"); foreach (sort {$a->[1] <=> $b->[1]} @files) { print $_->[0], "\n"; }
        As you can see, within the wanted function, we collect each filename and the time it was last modified. Once find() finishes, we then sort the files into the desired order and print them out.
        See the preprocess argument -- if you pass a code reference, it'll apply it to the list returned by readdir before calling your wanted coderef. Something like this, perhaps:
        find({ wanted => \&wanted, preprocess => sub { sort { -M $a <=> -M $b } @_; }, }, '.');
Re: How does find() build its tree.
by Fletch (Bishop) on Jan 28, 2002 at 20:39 UTC

    When in doubt, RTFS. Specifically, _find_opt and _find_dir are what does the real work.

    less `perl -MFile::Find -le 'print $INC{"File/Find.pm"}'`
      Ouch. perldoc -m File::Find.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://142096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-25 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found