-   Linux - General (
-   -   Shell Command to Find Newest File in Filesystem (

newmanium2001 04-13-2010 05:01 PM

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:

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?


rweaver 04-13-2010 05:14 PM

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.


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

newmanium2001 04-14-2010 09:59 AM

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.

rweaver 04-14-2010 11:18 AM

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.)

All times are GMT -5. The time now is 12:13 PM.