mtime.max

D

Derek Smith

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!

<snip>
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
 
D

Derek Smith

Derek said:
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!

<snip>
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
 
P

Paul Smith

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!

<snip>
d=3DDate.today
t=3DTime.now
require 'enumerator'
=A0 =A0stats =3D Hash.new
=A0 =A0DB_BKUP1.each do |dbfile|
=A0 =A0 =A0 =A0stats[dbfile] =3D [File.stat(dbfile).mtime.to_i]
=A0 =A0end
=A0 =A0puts stats.to_a,"\n\n"
=A0 =A0puts 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
271455.

instead, try

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

This explicitly sorts by the 2nd value in the to_a array, which is the mtim=
e.

--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
D

Derek Smith

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[1]} < #{oldest}
oldest = statsA[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]
 
D

Daniel Schömer

Hi!

Derek said:
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.

[...]
DB_BKUP1.each do |dbfile|
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
 
D

Derek Smith

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
 
R

Robert Klemme

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/
 
R

Robert Klemme

2009/10/31 Paul Smith said:
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!

<snip>
d=3DDate.today
t=3DTime.now
require 'enumerator'
=A0 =A0stats =3D Hash.new
=A0 =A0DB_BKUP1.each do |dbfile|
=A0 =A0 =A0 =A0stats[dbfile] =3D [File.stat(dbfile).mtime.to_i]
=A0 =A0end
=A0 =A0puts stats.to_a,"\n\n"
=A0 =A0puts stats.to_a.max

This line is sorting the hash by the key, i.e. the filename. =A0You'll
see that Tue is alphabetically after Thu and Fr, and 271642 is after
271455.

instead, try

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

This explicitly sorts by the 2nd value in the to_a array, which is the mt=
ime.

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


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
B

Brian Candler

Derek said:
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]}"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top