Operating System - Linux
1753405 Members
7323 Online
108793 Solutions
New Discussion юеВ

Re: Find the most recent file

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

Find the most recent file

Hello

How to find the most recent files (Created or modified) under a specified directory and recursively in sub dirs ?

any Idea ?

Thank you very much
Den
10 REPLIES 10
smatador
Honored Contributor

Re: Find the most recent file

Hi,
You could do for example
find /directory -ctime 1 -o -mtime 1 -depth -type f | xargs ls -al
Leo The Cat
Regular Advisor

Re: Find the most recent file

Hi

It's not enough because if no activities from more days ...

I need something more ...strongest to answer: THIS is the last modified or created file !!!

Do you see what I mean ?

Bests Regards
Den
Steven Schweda
Honored Contributor

Re: Find the most recent file

There might be some clever perl thing (about
which I know nothing), and it'd be pretty
easy to do in DCL, but "recursively" makes it
hard in a UNIX-like environment. (At least
_I_ don't see an easy way to do it.)

"ls -Rt --full-time" would give you all the
information you need, but not in a convenient
form. The directory names are on separate
lines, but the first line after "total"
would be the newest file in that directory.
Combine the directory name with the file
name, sort by the date-time field(s), and
you're done.

You could also do a "find -type d" to do the
directory recursion, and in each directory do
the "ls -t --full-time" to get the newest
file there. Collect all those, and sort them
by the date-time field(s).

It looks to me like too much work.

Why do you care?
smatador
Honored Contributor

Re: Find the most recent file

I don't try it yet but it seem very interesting. Look at this script
http://www.pixelbeat.org/scripts/newest
James R. Ferguson
Acclaimed Contributor
Solution

Re: Find the most recent file

Hi Den:

I'll use Perl for the reason that Steven cited.

There is _NO_ such thing as a creation timestamp in Unix or Linux. The 'ctime' field of the stat() structure is the last _change_ timestamp for an inode. Changing the permissions, ownership or name of a file or directory alters this timestamp. Writing to a file alters the 'mtime'.

This Perl script will return _files_ in the order of their last modification timestampe (or 'mtime'). Specifiy one or more directories to recursively search. If no arguments are given, your current working directory will be used. The files are sorted in descending age (newest first) thus allowing you to (conveniently) take the 'head' of the list returned:

# cat ./fileage
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my @a;
my @paths = @ARGV ? @ARGV : ('.');
find( sub { push @a, [ $File::Find::name, ( stat($_) )[9] ] if -f $_ },
@paths );
for ( sort { $b->[1] <=> $a->[1] } @a ) {
print join " ", scalar localtime( @$_[1] ), @$_[0], "\n";
}
1;

# ./fileage /root |head -3
Tue Apr 28 14:26:45 2009 /root/.sh_history
Sat Apr 25 14:40:02 2009 /root/bigfile
Thu Apr 16 15:18:21 2009 /root/jrfsig.pl

Regards!

...JRF...


Mike Stroyan
Honored Contributor

Re: Find the most recent file

You could use the stat command to get modification time in a convenient format.
Then the sort command would be able to order the files by time.

find . -exec stat -c '%Y %n' {} \; | sort -n | less

There may be many files with the same modification time.
One second resolution is actually a very long time for a fast computer.
Mark McDonald_2
Trusted Contributor

Re: Find the most recent file

find . -exec ls -lt {} \; | grep -v "^total" | head -1

Then you can use cut or awk to get only the file name.

Mark McDonald_2
Trusted Contributor

Re: Find the most recent file

Sorry - that will give dirs too, so:

find . -exec ls -lt {} \; | grep -v "^total" | grep -v "^d" | head -1

Then you can use cut or awk to get only the file name.

Mark McDonald_2
Trusted Contributor

Re: Find the most recent file

Sorry - It worked on a small structure I created (maybe by fluke) but doesnt work on larger numbers of files.