rubyunixfile-organization

How do I organize files by creation date?


How would I sort a directory into files created before and after any given time and date?

I need to make two lists, one of files before, and the other of files after, a certain date/time.


Solution

  • The creation time with Ruby on OS X is not available through the File API. One way is shelling out to stat(1). Not pretty but does at least return the creation (a.k.a birth) time:

    def birth(file)
      Time.at(`stat -f%B "#{file}"`.chomp.to_i)
    end
    
    Dir.entries('.').sort_by {|f| birth f }
    

    Or use the partition answer given.

    Here's a detailed post on a common misconception: ctime does not mean creation time.