LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 04-13-2010, 04:01 PM   #1
newmanium2001
LQ Newbie
 
Registered: Apr 2009
Posts: 14

Rep: Reputation: 0
Shell Command to Find Newest File in Filesystem


Hi there,

I'm relatively experienced with UNIX and Linux, but this has me thrown for quite a loop, and it seemed like such a simple question. How would I go about finding the newest file in a file system?

I thought something like:
Code:
ls -ltr `find /usr -type f`
would work, but I seem to be exceeding the argument maximum for ls:

ksh: 0403-029 There is not enough memory available now

I thought something involving xargs might work, but I really suck with that command. Anyone got any ideas?

Thanks!
 
Old 04-13-2010, 04:14 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
You can try this out, it should work fairly well... right now it returns the 5 most recent and gives an ls on them... you could change that number by changing the head items returned.

Code:
for i in $(find / -printf "%T@ %Tx %TX %p\n" | sort -n -r | head -5 | cut -f5 -d' '); do ls -al $i; done
If you're looking for just something to run at command line that is simple...

find / -printf "%T@ %Tx %TX %p\n" | sort -n -r | head -5

Last edited by rweaver; 04-13-2010 at 04:20 PM.
 
1 members found this post helpful.
Old 04-14-2010, 08:59 AM   #3
newmanium2001
LQ Newbie
 
Registered: Apr 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Excellent! That works quite well (correction: on Linux).

One more thing though ... do you think you could explain to me that each of those %T specifiers does? It's hard to find good documentation about printf and I'm not familiar with those specifiers.

Edit: Seems that the -printf does not exist in AIX find. Was looking for something a little more universal:

find: 0652-017 -printf is not a valid option.

Last edited by newmanium2001; 04-14-2010 at 09:04 AM.
 
Old 04-14-2010, 10:18 AM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Ah the joys of working on cross platforms. No, that option isn't universal, likely you'd have to source compile find to get that functionality on AIX (or irix for that matter, I think it works on current solaris and bsd.) That being said there is no universal answer that will work everywhere, because the tools aren't uniform, the base functionality is typically the same, but the additional features can vary quite drastically... without using printf I'd try using -mtime -1 and try to figure a way to sort that list further. I don't have an aix system to tinker with at the moment to check options against heh.

In the case of what do the printf functions do, check out the ~find~ man page instead of the printf man page, you'll also get more useful information out of 'man 3 printf' than 'man 1 printf' typically (the section 1 manpage of printf is pretty worthless.)
 
  


Reply

Tags
find, scripting, shell, sort


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find newest file capella Linux - Software 9 04-28-2012 04:26 AM
KNOPPIX can't find filesystem, dropping to limited shell gpsfreak DamnSmallLinux 1 12-01-2009 02:34 PM
Quickly find the newest file in a directory yorkshiresteve Linux - General 2 02-15-2009 11:32 AM
Find Icon File in linux filesystem acmeinc Linux - General 8 10-06-2008 03:20 AM
Where can i find the MySQL database file in Linux Filesystem dc_eros Slackware 4 05-09-2007 11:54 AM


All times are GMT -5. The time now is 10:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration