I feel like Im smoking crack and getting pissed off at the same time! No
offence intended!
This is still not working as I should be seeing
/backups/prod_db_bkup_Thr2009-10-291052.gz
as the oldest file.
=A0require 'time'
=A0 =A0stats =3D Hash.new
=A0 =A0DB_BKUP1.each do |dbfile|
=A0 =A0 =A0 =A0stats[dbfile] =3D [File.stat(dbfile).mtime]
=A0 =A0end
=A0 =A0stats_sorted =3D Hash.new
=A0 =A0stats_sorted =3D stats.sort { |x,y| x[1]<=3D>y[1] }
=A0 =A0oldest =3D stats_sorted[0][1].first
=A0 =A0p oldest
=A0 =A0oldestfile =3D ''
=A0 =A0stats.each_value do |v|
=A0 =A0 =A0 =A0if =A0#{v} < #{oldest}
=A0 =A0 =A0 =A0 =A0 =A0oldest =3D v
=A0 =A0 =A0 =A0 =A0 =A0oldestfile =3D stats.index(oldest)
=A0 =A0 =A0 =A0end
=A0 =A0end
=A0 =A0puts oldestfile
=A0 =A0puts "exit"
=A0 =A0exit
end
prod_DB_OS_bkup.rb: 108 lines, 2997 characters.
[root@.../usr/local/vrep/OS_scripts]# ruby prod_DB_OS_bkup.rb
Thu Oct 29 10:52:41 -0400 2009
/backups/prod_db_bkup_Thr2009-10-291054.gz
exit
[root@.../usr/local/vrep/OS_scripts]# ls -alrt /backups/prod*.gz
-rw-r--r-- =A01 root =A0wheel =A038608780 Oct 29 10:52
/backups/prod_db_bkup_Thr2009-10-291052.gz
-rw-r--r-- =A01 root =A0wheel =A038608793 Oct 29 10:54
/backups/prod_db_bkup_Thr2009-10-291054.gz
-rw-r--r-- =A01 root =A0wheel =A038608802 Oct 29 11:00
/backups/prod_db_bkup_Thr2009-10-29110.gz
-rw-r--r-- =A01 root =A0wheel =A038609193 Oct 30 10:42
/backups/prod_db_bkup_Fri2009-10-301042.gz
-rw-r--r-- =A01 root =A0wheel =A038770872 Nov =A01 16:29
/backups/prod_db_bkup_Sun2009-11-011629.gz
IMHO you're making your life too difficult: for the youngest file you
can use Enumerable#max_by:
09:25:28 Temp$ ls -lt | head -5
total 339
-rwx------+ 1 RKlemme Domain Users 1544 Nov 2 09:25
etilqs_y5Wb4Ntehb25BGev1S84-journal*
-rwx------+ 1 RKlemme Domain Users 184862 Nov 2 09:14 xphoon.bmp*
drwx------+ 2 RKlemme Domain Users 0 Nov 2 09:14 notes6030C8/
drwxr-xr-x+ 2 RKlemme Domain Users 0 Nov 2 09:11 hsperfdata_rklemme/
09:26:07 Temp$ allruby -e 'puts Dir["*"].max_by {|f| File.mtime f}'
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
etilqs_y5Wb4Ntehb25BGev1S84-journal
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
etilqs_y5Wb4Ntehb25BGev1S84-journal
For the oldest it is Enumerable#min_by:
09:26:41 Temp$ ls -lt | tail -5
-rwx------+ 1 RKlemme Domain Users 663 Feb 6 2009 to_h.rb*
-rwxr--r-- 1 RKlemme Domain Users 548 Jan 27 2009 typo-finder.rb*
-rwx------+ 1 RKlemme Domain Users 390 Jan 20 2009 req.rb*
-rwx------+ 1 RKlemme Domain Users 426 Dec 11 2008 ds.rb*
-rwx------+ 1 RKlemme Domain Users 787 Dec 9 2008 drb-demo.rb*
09:26:47 Temp$ allruby -e 'puts Dir["*"].min_by {|f| File.mtime f}'
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
drb-demo.rb
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
drb-demo.rb
Even if you want the whole array sorted by mtime you can use #sort_by
and #last or #first:
09:26:53 Temp$ allruby -e 'ar =3D Dir["*"].sort_by {|f| File.mtime f};
puts ar.first, ar.last'
CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
drb-demo.rb
etilqs_y5Wb4Ntehb25BGev1S84-journal
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
drb-demo.rb
etilqs_y5Wb4Ntehb25BGev1S84-journal
Kind regards
robert
--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/