Listing latest modified or created files recursively

Hi,

I want to display latest files (created or modified) recursively in a path.

I tried in different ways, but didn't get any desired output:

find $path -type f -exec ls -lt {} \; | sort -n -r
find $path -type f -printf %p";" | xargs -d ";" ls -t

Second one is giving the error:

xargs: The -d flag is not valid.
Usage: xargs [-p][-t] [-e[EndOfFileString]] [-E EndOfFileString]
[-I ReplacementString] [-i[ReplacementString]] [-L Number]
[-l[Number]] [-n Number [-x]] [-s Size] [Command [Argument ...]]
find: 0652-017 -printf is not a valid option.

Any other way to display the latest files recursively ?

Thanks.

i think ls -lt is enough. no need to sort it again..

But we need to find it recursively.
ls -lt will list from purticular directory.

latest last:

ls -ltr

Is it giving the output recursively for you ?

 
ls -ltRr

I tried this one too. It gave directory by directory sorted output.

In short, my question is: Finding the latest modified or created file from entire drive which have number of folders and sub folders.

From current directory try:

date +%Y | read cy
find ./ -type f -exec ls -l {} \; | awk -v cy=$cy '
BEGIN{
 ms="JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
 split(ms,ma,",");
 for (i=1; i<=12; i++) mo[ma]=i;
}
{
 l=$0;
 sub("  *[.][/].*","",l);
 tl=l;
 gsub("  *"," ",l);
 c=split(l,a);
 t="0000";
 y=a[c];
 if (a[c] ~ ":") {
    t=a[c];
    gsub(":","",t);
    y=cy;
 }
 m=a[c-2];
 m=toupper(m);
 d=a[c-1];
 if (mo[m] ~ /^$/) mo[m]="01";
 fl=$0;
 sub(tl,"");
 printf("%4d%02d%02d%s : %s\n", y,mo[m],d,t,$0);
}
' | sort -n

NOTE: The find will run for a while from top level dir.

Just like most people seeking help, you neglected to specify your operating system. Without that information we are forced to guess or we must limit ourselves to a less functional common denominator.

Your first post suggests you are not using a GNU/Linux system, otherwise -printf would have been recognized. Had it been supported, it could have accomplished the decoration in a decorate-sort-dedecorate solution:

find . -type f -printf '%T@\t%p\n' | sort -rn | cut -f2-

Since that doesn't seem possible, we have to turn elsewhere. Many systems have a stat tool (or something similar) which can output numerically sortable timestamp data, but these utilities are highly unportable. With the proper options, such a tool could be invoked effectively by find's -exec.

Regards,
Alister

assuming your column 6,7,8 are month, date and time respectively...
try this...
latest at the last...:slight_smile:

find $path -type f -exec ls -lt {} \;  | sort -k 6 -k 7 -k 8

The ls date format is locale dependent. In what locale does your suggestion work? Can you show a sample of that locale's ls long-format output?

Since ls is never called with more than one argument, the -t option serves no purpose.

Regards,
Alister

I am running it on bash and as earlier mentioned in my previous post, ls format is month, date and time for columns 6,7 & 8 respectively. i have tried that and it works fine thats why i've suggested this...:slight_smile:

May be for other format we need to modify it lit bit..:slight_smile:

"locale dependent" means "what works for you won't work for someone else". Even worse, it will fail in ways that aren't obvious -- the output will be scrambled or wrong, rather than getting an error message or "option not supported".

It's better to write code that works in many places than make someone else fix code that doesn't.

As what you said "locale dependent". Okies.. that's fine. agreed..
But I'll try as per what resources I have. This is not possible to check for all other types if I don't have it.

There may be resources new to you then. :slight_smile: such as find's -printf command, which can print date and time as you want.

find . -printf "%AY%Am%Ad%AH%AM %p\n" | sort -n | sed 's/^[0-9]* //'

see the main first post. as he tried using sort that's why i have used this...

Garbage in, garbage out. Problem was not with sort but the manner it was being used and the data it was given.

I made find print timestamps in YYYYMMDDHHMM, because dates given in that format always sort perfectly if you put them on the beginning of the line.

That's why i used positional sort. this is as per what i got from find and ls.

...and we have now come full circle.

"Locale dependent" means "what works for you won't work for someone else". Even worse, it will fail in ways that aren't obvious -- the output will be scrambled or wrong, rather than getting an error message or "option not supported". ls' output on even the same machine can be different for different files. For new files, my version of ls shows dates; for older ones, it shows only years. You can't sort that.

Using find -prinf means you always get exactly what you ask for, no more, no less.

Yes. agreed...
but till the time you don't know what is result of the command we can't comment anything. max we can request user to give proper inputs but this never happens.
as i am not master you do expect post like this.:slight_smile:
ps: i m not fighting over which method is good...