# Ruby scrip to find Dir based on last modified date

**URL:** <https://www.ruby-forum.com/t/ruby-scrip-to-find-dir-based-on-last-modified-date/199004>\
**Category:** Ruby\
**Created:** [November 12, 2010, 6:41am UTC](https://www.ruby-forum.com/t/ruby-scrip-to-find-dir-based-on-last-modified-date/199004 "2010-11-12T06:41:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 12, 2010, 6:41am UTC](https://www.ruby-forum.com/t/ruby-scrip-to-find-dir-based-on-last-modified-date/199004/1 "2010-11-12T06:41:48Z")

</div>

Hi  
Can someone here please help/guide me how I can implement the following  
scenario.

Scenario:

A directory on my Mac has list of Sub-directories, What I need is Ruby  
script that finds the Sub Directory that was Last Modified based on the  
modified date and then copy that Sub Directory and it is content to my  
Different Dir on my Mac.

Appreciate your much needed help

bal

---

<div class="post-metadata">

**Author:** ![balvirs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@balvirs](https://www.ruby-forum.com/u/balvirs)\
**Post date:** [November 12, 2010, 10:02pm UTC](https://www.ruby-forum.com/t/ruby-scrip-to-find-dir-based-on-last-modified-date/199004/2 "2010-11-12T22:02:53Z")

</div>

Robert K. wrote in post #960918:

> On Fri, Nov 12, 2010 at 6:41 AM, Bal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Appreciate your much needed help
> 
> This should get you started:
> 
> irb(main):001:0\> Dir[“_“].select {|f| test ?d, f}  
> =\> [“a1”, “bin”, “desktop”, “doc”, “lib”, “ruby”, “vimfiles”]  
> irb(main):002:0\> Dir[”_”].select {|f| test ?d, f}.sort\_by {|f|  
> File.mtime f}  
> =\> [“lib”, “vimfiles”, “ruby”, “a1”, “doc”, “bin”, “desktop”]
> 
> Cheers
> 
> robert

Hi Robert  
Thanks for you reply, much appreciated, above command work fine, seems  
it need further change, This is what I am trying to achieve,

e.g. Below is list of Directories exists in my home Dir, say my home Dir  
is /home/myhome/

drwxr-xr-x 4 testuser staff 136 Oct 19 15:27 .argouml  
drwxr-xr-x 7 testuser staff 238 Aug 25 15:02 .yed3  
drwxr-xr-x 23 testuser staff 782 Aug 12 13:23 enli.x

using ruby script I lik to find Dir with latest time stamp and then copy  
the that Dir to diff Dir e.g. /usr/bin/

I google to see if their is ruby method exist to find scan the Dir then  
find the one with latest time stamp, i couln’t find anything,

any thoughts ?

thanks

---

<div class="post-metadata">

**Author:** ![balvirs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@balvirs](https://www.ruby-forum.com/u/balvirs)\
**Post date:** [November 13, 2010, 11:10am UTC](https://www.ruby-forum.com/t/ruby-scrip-to-find-dir-based-on-last-modified-date/199004/3 "2010-11-13T11:10:49Z")

</div>

On 11/12/2010 10:03 PM, Bal S. wrote:

> > irb(main):002:0\> Dir["\*"].select {|f| test ?d, f}.sort\_by {|f|  
> > File.mtime f}  
> > =\> [“lib”, “vimfiles”, “ruby”, “a1”, “doc”, “bin”, “desktop”]

> using ruby script I lik to find Dir with latest time stamp and then copy  
> the that Dir to diff Dir e.g. /usr/bin/
> 
> I google to see if their is ruby method exist to find scan the Dir then  
> find the one with latest time stamp, i couln’t find anything,
> 
> any thoughts ?

Use #max\_by. And look into FileUtils for the copying.

robert

---

<div class="post-metadata">

**Author:** ![balvirs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e8cbc/32.png) [@balvirs](https://www.ruby-forum.com/u/balvirs)\
**Post date:** [November 12, 2010, 10:20am UTC](https://www.ruby-forum.com/t/ruby-scrip-to-find-dir-based-on-last-modified-date/199004/4 "2010-11-12T10:20:52Z")

</div>

On Fri, Nov 12, 2010 at 6:41 AM, Bal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Appreciate your much needed help

This should get you started:

irb(main):001:0\> Dir[“_“].select {|f| test ?d, f}  
=\> [“a1”, “bin”, “desktop”, “doc”, “lib”, “ruby”, “vimfiles”]  
irb(main):002:0\> Dir[”_”].select {|f| test ?d, f}.sort\_by {|f|  
File.mtime f}  
=\> [“lib”, “vimfiles”, “ruby”, “a1”, “doc”, “bin”, “desktop”]

Cheers

robert
