<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial,helvetica,sans-serif;font-size:12pt">Actually, I just wanted to find the time, not the file, so by removing the file name, life gets really simple.<br><br>I agree, there are just so many tricks out there. Yes, Google is a good place to start, but at times I've spent hours researching, given up, then asked this list and get an answer in minutes. The amount of Linux experience we have online in TCLUG is huge and just having someone to help reduce the subject area helps. Besides, we all learn a little with these inquiries. I consider myself a pretty experienced programmer and admin, but sometimes even I don't know the answer, or like to hear how others have solved the problem. There is always some new trick, that's what makes Linux/UNIX so fun.<br><br>Thanks all.<br><br><div> </div><span style="font-family:
courier;">--- </span><br style="font-family: courier;"><span style="font-family: courier;">Wayne Johnson, | <span style="color: rgb(191, 95, 0);">There are two kinds of people: Those</span> </span><br style="font-family: courier;"><span style="font-family: courier;">3943 Penn Ave. N. | <span style="color: rgb(191, 95, 0);">who say to God, "Thy will be done," </span></span><br style="font-family: courier;"><span style="font-family: courier;">Minneapolis, MN 55412-1908 | <span style="color: rgb(191, 95, 0);">and those to whom God says, "All right, </span></span><br style="font-family: courier;"><span style="font-family: courier;">(612) 522-7003 | <span style="color: rgb(191, 95, 0);">then, have it your way." --C.S.
Lewis</span><br></span><div style="font-family: arial,helvetica,sans-serif; font-size: 12pt;"><br><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">----- Original Message ----<br>From: Mike Miller <mbmiller@taxa.epi.umn.edu><br>To: TCLUG List <tclug-list@mn-linux.org><br>Sent: Wednesday, January 9, 2008 11:40:37 PM<br>Subject: Re: [tclug-list] Finding the date of the newest file in a directory tree<br><br>
On Wed, 9 Jan 2008, Florin Iucha wrote:<br><br>> And the Oscar goes to:<br>><br>> find /some/dir -type f -printf "%h/%f %T@\n" | awk '{ if ($2 >
the_max) { the_max = $2; file_name = $1; } }<br>> END { print file_name }'<br>><br>> I would like to thank Google for its search engine and to the find
man <br>> page for its thorough description of the million options and
switches...<br><br><br>This is the stuff I like most on LUG lists -- learning all the cool
tricks <br>with GNU/UNIX/Linux commands. So much can be done but it takes years
to <br>learn all the efficient ways of doing things. I've used awk/gawk a <br>gazillion times but only in a few ways, so using it to find a maximum
was <br>not in my repertoire, but that is an excellent idea. I always would
have <br>sorted the file even though I knew that couldn't be the best way to go.<br><br>That said, there are still some problems with the one-liner above.
First <br>and foremost, if any file in the tree contains a space in the filename,
<br>the command will fail. At first I was going to say that the problem is
in <br>the printf argument because it doesn't uses a space as delimiter
between <br>the file name and date stamp:<br><br>$ find . -type f -printf "%h/%f %T@\n"<br>./Lee, Alvin - I'm Going Home.txt 1182200822<br>./0_TABLATURE_EXPLANATION.txt 1118104853<br>./Semisonic - FNT.txt 1153491460<br>./Animals - House of the Rising Sun.tab.txt 1142214281<br>[snip]<br><br>But maybe it is better to say that the problem is with the awk command.
<br>If we replace $2 with $NF and replace $1 with $0, we get this:<br><br>find /some/dir -type f -printf "%h/%f %T@\n" | awk '{ if ($NF >
the_max) { the_max = $NF; file_name = $0; } }<br>END { print file_name }'<br><br>But the problem with that is that it retains the date stamp at the end <br>like so:<br><br>./Lee, Alvin - I'm Going Home.txt 1182200822<br><br>But that can be removed by adding a little perl (or sed) regexp thingy
at <br>the end:<br><br>find /some/dir -type f -printf "%h/%f %T@\n" | awk '{ if ($NF >
the_max) { the_max = $NF; file_name = $0; } }<br>END { print file_name }' | perl -pe 's/^(.+) [0-9]+$/$1/'<br><br>That will run almost exactly as fast as the earlier suggestion because
the <br>perl bit at the end is very fast and it is only done on the single line
of <br>output at the end. On the other hand, you didn't say that you wanted
the <br>filename, you said that you wanted the date. That simplifies things a <br>bit! You can do this:<br><br>find /some/dir -type f -printf "%T@\n" | awk '{ if ($1 > the_max) {
the_max = $1; } } END { print the_max }'<br><br>That returns the modification date of the newest file in seconds since <br>1970-01-01 00:00:00 UTC. If you want a different date format, we can <br>discuss that. There must be a good trick. You can get the current
time <br>in that format using the date command as follows:<br><br>date +%s<br><br>There are other forms of weirdness with UNIX filenames, like they can <br>include a newline, and that will also mess you up, but maybe that never
<br>happens on your system (and if you and your users and your software are
<br>all sane, it won't happen!).<br><br>Do you want to find the newest file as of the moment your script starts
<br>running, or will you want to detect new files that are created after
the <br>script starts running but before it finishes? Maybe this isn't an <br>important consideration for you, but you should be aware that what you <br>mean by the "newest file" isn't defined precisely by the method you are
<br>using to identify it.<br><br>Best,<br><br>Mike<br><br>_______________________________________________<br>TCLUG Mailing List - Minneapolis/St. Paul, Minnesota<br><a ymailto="mailto:tclug-list@mn-linux.org" href="mailto:tclug-list@mn-linux.org">tclug-list@mn-linux.org</a><br><a href="http://mailman.mn-linux.org/mailman/listinfo/tclug-list" target="_blank">http://mailman.mn-linux.org/mailman/listinfo/tclug-list</a><br></div><br></div></div><br>
<hr size=1>Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. <a href="http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ "> Try it now.</a></body></html>