- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Find the most recent file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 05:44 AM
тАО04-28-2009 05:44 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 07:03 AM
тАО04-28-2009 07:03 AM
Re: Find the most recent file
You could do for example
find /directory -ctime 1 -o -mtime 1 -depth -type f | xargs ls -al
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 10:08 AM
тАО04-28-2009 10:08 AM
Re: Find the most recent file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 11:14 AM
тАО04-28-2009 11:14 AM
Re: Find the most recent file
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 11:50 AM
тАО04-28-2009 11:50 AM
Re: Find the most recent file
http://www.pixelbeat.org/scripts/newest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 03:41 PM
тАО04-28-2009 03:41 PM
SolutionI'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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 08:50 PM
тАО04-28-2009 08:50 PM
Re: Find the most recent file
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 11:07 PM
тАО04-28-2009 11:07 PM
Re: Find the most recent file
Then you can use cut or awk to get only the file name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 11:10 PM
тАО04-28-2009 11:10 PM
Re: Find the most recent file
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-28-2009 11:16 PM
тАО04-28-2009 11:16 PM