Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Think about Loose Coupling
 
PerlMonks  

Order of files returned by readdir

by jpfarmer (Pilgrim)
on Mar 01, 2006 at 15:15 UTC ( [id://533744]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I have an application where I use readdir to iterate over the files in a directory handle opened by opendir. Perldoc gives the following explanation of how the function works:

Returns the next directory entry for a directory opened by "opendir".

However, it does not specify how "next" is determined. From my own quick testing, it appears that the order is consistent, but I have not been able to discern how the order is derived and it does not appear to be documented anywhere.

Do any of you know how the order is determined?

Replies are listed 'Best First'.
Re: Order of files returned by readdir
by Fletch (Bishop) on Mar 01, 2006 at 15:20 UTC

    However your OS' underlying readdir() routine returns them. My copy of Stevens (APUE, p108) says that the ordering is implementation dependent and usually not alphabetical. Safest thing to do if you want a particular ordering is to sort the results yourself after the fact.

Re: Order of files returned by readdir
by GrandFather (Saint) on Mar 01, 2006 at 15:55 UTC

    Actually the order won't even be consistent. For example, deleting an entry then adding another will likely change the order somewhat. The order depends on the operating system and the file system that is being used.

    With Windows for example, readdir most likely uses FindFirstFile/FindNextFile which don't specify the order files are returned.


    DWIM is Perl's answer to Gödel
Re: Order of files returned by readdir
by Argel (Prior) on Mar 01, 2006 at 20:48 UTC
    Perl's readdir is for the most part a frontend/wrapper for an OS system call (readdir on UNIX) so don't expect sugar-coated responses. On Solaris (and probably other UNIX varients) you will get '.' and '..' back as well. If I was going to assume anything it would be that it returns the contents of the directory in the order they were created in (i.e. like a queue). But as previosuly mentioned, sort it yourself if you need a specific order.
      On Solaris (and probably other UNIX varients) you will get '.' and '..' back as well.

      On Windows as well.


      --
      Regards,
      Helgi Briem
      hbriem AT f-prot DOT com
Re: Order of files returned by readdir
by superfrink (Curate) on Mar 01, 2006 at 21:56 UTC
    The entries are probably returned in whatever order the implementor figured would be the fasted order to return them.

    Traditionally Unix filesystems store a list files and directories in an unsorted list. Think of it as an array. The fasted way to return the items is to just loop over the array.

    A fast way to insert an item into the array is to insert in the next unused slot. Suppose the filesystem does not keep an index that points to the next free slot so the system just loops over the array until it finds a free slot.

    A fast way to remove an item is to loop over the array to find the item and then just mark that slot as unused. The system could sort the array when an item is removed but that takes time so it likely just leaves an open slot wherever an item is removed.

    There are filesystems (like ReiserFS) that use trees for indexing to give faster searching. In the end the order that items are returned by readdir is not defined to be sorted in any particular way so it's up to the application to sort the items as required.
      Personally I view this as a feature - at work I deal with directories containing thousands of files, and often I want to just count them or do something else which is order-independent. So rather than use ls, which always sorts its output somehow, I've written a tiny perl script which just returns the list of files as given by readdir().
        man ls ... -f do not sort, enable -aU, disable -lst
Re: Order of files returned by readdir
by leocharre (Priest) on Mar 06, 2006 at 09:14 UTC

    You need to sort them. If you feed them into an array..

    use strict; my @f = qw(leo and magalena went down to the basement to get some good + scotch they had hidden from their uncle); @f = sort {$a cmp $b} @f; for (@f){ print "$_\n";}

    This will sort alphabetically. You could also have done a stat on these files and fed all into a hash, then you compare the key values.

    Do a search right here on perlmonks for "directory listing" and you may find all kinds of goodies.

    If this is *not* going to be a cgi, then yes you can do a call to the system by this for example:

    my @sorted_files = split(/\n/, `ls /home/myself/ -S -r`);

    This lists everything in my homedir by size in reverse, so... smallest files first. of course, this is no longer perl. Don't get used to backticks if you plan on doing serious webstuff.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://533744]
Approved by GrandFather
Front-paged by astaines
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.