On Thu, 10 Jan 2008, Florin Iucha wrote:
> We can switch the arguments around and use a less likely separator
> (warning, untested):
>
> find /some/dir -type f -printf "%T@^%h/%f\n" | awk -F^ '{ if ($1 > the_max) { the_max = $1; file_name = $2; } }
> END { print file_name }'
That will fail only if the newest file has a caret (^) in the filename,
but you can deal with that by printing the full line and using "cut" to
parse it:
find /some/dir -type f -printf "%T@^%h/%f\n" | awk -F^ '{ if ($1 > the_max) { the_max = $1; file_name = $0; } }
END { print file_name }' | cut -d^ -f 2-
That seems to work. It gives the filename as output instead of the date.
This makes me want to write a little script for finding the newest file
with "some/dir" given as input on the command line. I do need this kind
of thing sometimes. I think I would do it by having a fast and slow form
where the fast version used awk and only printed the newest file, but the
slow version used sort and printed the n newest files along with the
number of days, minutes and seconds since they were modified.
When I'm doing this in a single directory, I just do this:
ls -lAFtc | head -n
where the "n" is replaced by the number of newest files I want to see.
The "t" argument to ls forces it to sort by date and the "c" argument
makes it use the real modification time instead of the date stamp (which
can be changed by "touch"). If someone breaks into your machine and
modifies files they'll often change date stamps to make it seem like the
files were not modified recently, so "ls -lc" will reveal this.
Mike