Mtime.max

Hi All,

My goal is to get the oldest file using mtime.to_i then delete it.
However my
puts stats.to_a.max does not seem to print out the oldest mtime
data/file, rather its printing out the 2nd to oldest file;
prod_db_bkup_Tue2009-10-271455.gz

Correct me please and show me the code.

thx!

d=Date.today t=Time.now require 'enumerator' stats = Hash.new DB_BKUP1.each do |dbfile| stats[dbfile] = [File.stat(dbfile).mtime.to_i] end puts stats.to_a,"\n\n" puts stats.to_a.max end prod_DB_OS_bkup.rb: 90 lines, 2627 characters. [root@vixxxxx /usr/local/vrep/OS_scripts]# ruby prod_DB_OS_bkup.rb /usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz 1256828444 /usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz 1256828056 /usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz 1256913745 /usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz 1256827961 /usr/local/vrep/prod_db_bkup_Tue2009-10-271455.gz 1256669730 /usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz 1256676168

/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
1256676168

Derek S. wrote:

Hi All,

My goal is to get the oldest file using mtime.to_i then delete it.
However my
puts stats.to_a.max does not seem to print out the oldest mtime
data/file, rather its printing out the 2nd to oldest file;
prod_db_bkup_Tue2009-10-271455.gz

Correct me please and show me the code.

thx!

d=Date.today t=Time.now require 'enumerator' stats = Hash.new DB_BKUP1.each do |dbfile| stats[dbfile] = [File.stat(dbfile).mtime.to_i] end puts stats.to_a,"\n\n" puts stats.to_a.max end prod_DB_OS_bkup.rb: 90 lines, 2627 characters. [root@vixxxxx /usr/local/vrep/OS_scripts]# ruby prod_DB_OS_bkup.rb /usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz 1256828444 /usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz 1256828056 /usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz 1256913745 /usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz 1256827961 /usr/local/vrep/prod_db_bkup_Tue2009-10-271455.gz 1256669730 /usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz 1256676168

/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
1256676168

ls -alrt /usr/local/vrep/prod*
-rw-r–r-- 1 root wheel 38608031 Oct 27 14:55
/usr/local/vrep/prod_db_bkup_Tue2009-10-271455.gz
-rw-r–r-- 1 root wheel 38608186 Oct 27 16:42
/usr/local/vrep/prod_db_bkup_Tue2009-10-271642.gz
-rw-r–r-- 1 root wheel 38608780 Oct 29 10:52
/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz
-rw-r–r-- 1 root wheel 38608793 Oct 29 10:54
/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz
-rw-r–r-- 1 root wheel 38608802 Oct 29 11:00
/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz
-rw-r–r-- 1 root wheel 38609193 Oct 30 10:42
/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz

sorry meant to say .min not .max

On Sat, Oct 31, 2009 at 1:38 AM, Derek S.
[email protected] wrote:

thx!
puts stats.to_a.max
This line is sorting the hash by the key, i.e. the filename. You’ll
see that Tue is alphabetically after Thu and Fr, and 271642 is after

instead, try

puts stats.sort {|x,y| x[1] <=> y[1]}.max

This explicitly sorts by the 2nd value in the to_a array, which is the
mtime.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

Hi!

Derek S. schrieb:

    stats[dbfile] = [File.stat(dbfile).mtime.to_i]
end

[…]

Why do you convert the mtime (a Time object) to an integer? Time
objects are comparable. Try to omit the “.to_i” and compare the
Time objects that represent the mtimes.

Regards,
Daniel

Why do you convert the mtime (a Time object) to an integer? Time
objects are comparable. Try to omit the “.to_i” and compare the
Time objects that represent the mtimes.

Regards,
Daniel

HI!

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.

require ‘time’
stats = Hash.new
DB_BKUP1.each do |dbfile|
stats[dbfile] = [File.stat(dbfile).mtime]
end

stats_sorted = Hash.new
stats_sorted = stats.sort { |x,y| x[1]<=>y[1] }

oldest = stats_sorted[0][1].first
p oldest
oldestfile = ''
stats.each_value do |v|
    if  #{v} < #{oldest}
        oldest = v
        oldestfile = stats.index(oldest)
    end
end
puts oldestfile
puts "exit"
exit

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-- 1 root wheel 38608780 Oct 29 10:52
/backups/prod_db_bkup_Thr2009-10-291052.gz
-rw-r–r-- 1 root wheel 38608793 Oct 29 10:54
/backups/prod_db_bkup_Thr2009-10-291054.gz
-rw-r–r-- 1 root wheel 38608802 Oct 29 11:00
/backups/prod_db_bkup_Thr2009-10-29110.gz
-rw-r–r-- 1 root wheel 38609193 Oct 30 10:42
/backups/prod_db_bkup_Fri2009-10-301042.gz
-rw-r–r-- 1 root wheel 38770872 Nov 1 16:29
/backups/prod_db_bkup_Sun2009-11-011629.gz

puts stats.sort {|x,y| x[1] <=> y[1]}.max

This explicitly sorts by the 2nd value in the to_a array, which is the
mtime.

Thanks, but that does not work because its sorting on the time only, for
example sorting on 1052, 1054, 110, 1042.

root wheel 38608780 Oct 29 10:52
/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz
root wheel 38608793 Oct 29 10:54
/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz
root wheel 38608802 Oct 29 11:00
/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz
root wheel 38609193 Oct 30 10:42
/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz

so I am using this, but I could not get my each loop to work
please help!

stats = Hash.new
DB_BKUP1.each do |dbfile|
stats[dbfile] = [File.stat(dbfile).mtime.to_i]
end

statsA     = stats.sort { |x,y| x[1] <=> y[1] }.to_a
oldest     = statsA[0][1].to_s.to_i

p statsA
p "1st oldest:, oldest
(0..statsA.length-1).step(1) do |i|
    if #{statsA[i][1]} < #{oldest}
        oldest = statsA[i][1]
    end
end
p "after if clause oldest:", oldest

OUTPUT

[["/usr/local/vrep/prod_db_bkup_Thr2009-10-291052.gz", [1256827961]],
["/usr/local/vrep/prod_db_bkup_Thr2009-10-291054.gz", [1256828056]],
["/usr/local/vrep/prod_db_bkup_Thr2009-10-29110.gz", [1256828444]],
["/usr/local/vrep/prod_db_bkup_Fri2009-10-301042.gz", [1256913745]]]

“1st oldest:”
[1256827961]
“after if clause oldest:”
[1256913745]

2009/11/2 Derek S. [email protected]:

Why do you convert the mtime (a Time object) to an integer? Time
objects are comparable. Try to omit the “.to_i” and compare the
Time objects that represent the mtimes.

   stats[dbfile] = [File.stat(dbfile).mtime]
       oldest = v

Thu Oct 29 10:52:41 -0400 2009
-rw-r–r-- 1 root wheel 38609193 Oct 30 10:42
/backups/prod_db_bkup_Fri2009-10-301042.gz
-rw-r–r-- 1 root wheel 38770872 Nov 1 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

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
etilqs_y5Wb4Ntehb25BGev1S84-journal

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

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
drb-demo.rb

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 = 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

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
drb-demo.rb
etilqs_y5Wb4Ntehb25BGev1S84-journal

ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
drb-demo.rb
etilqs_y5Wb4Ntehb25BGev1S84-journal

Kind regards

robert

Derek S. wrote:

My goal is to get the oldest file using mtime.to_i then delete it.

How about:

stats = DB_BKUP1.map { |dbfile| [dbfile, File.stat(dbfile)] }
stats = stats.sort_by { |dbfile,stat| stat.mtime }
puts "Please rm #{stats.first[0]}"

2009/10/31 Paul S. [email protected]:

Correct me please and show me the code.
end

This explicitly sorts by the 2nd value in the to_a array, which is the mtime.

There is no point in first sorting and then using max. Only one of
the two makes sense: either you sort by your criterion and then take
the first or last OR you find the max or min according to your
criterion.

Kind regards

robert