Help with find command

Hello Gurus,

Please advise me with the below command on AIX.

I'm running this to list all the files of specific pattern such that they are sorted as per the time stamp. But I'm not able to achieve that with below command as it is not sorting the files as per time stamp

find / -name "*pattern*" -exec ls -ltr {} \;

Thanks for checking :slight_smile:

I am not sure why are you using -exec with ls -ltr..You can directly use like,

find / -name "*pattern*"  | ls -ltr

Hi sachinkl

find / -name "*pattern*" | ls -ltr 

is listing all the files in reverse order of time stamp and
not the files matching specific pattern.

The reason for me choosing the find command is I want to delete the files which are older than specific time stamp. For some reason I'm not able to use mtime option because that files will be accessed such that it will change the time stamp.

That's because find is exec'ing one ls per file that matches '*pattern'. There's nothing for it to sort since ls is given only one argument each time. The order you see is the order in which find found the files.

Any solution which depends on individual file names passed as command arguments is going to be limited by the system's exec syscall limits. If you're only dealing with a few thousand files or less, and if your find command supports the exec-+ syntax, you may be able to get by with the following:

find / -name '*pattern*' -exec ls -ltr {} +

[/code]

No, you cannot. ls does not read arguments from standard input.

Perhaps I misunderstood, but simply accessing a file should not affect it's modification timestamp.

In any case, if for some reason -mtime isn't working for you, and if you have a file with a reference timestamp, you can use -newer.

Regards,
Alister

i guess u need to use sort command further...

Thanks so much Alister for detailed explanation and it is working.

Can you please explain me what that + is ? Not able to find clear explanation in man page

In short, it tells find that instead of exec'ing once per matching pathname, it should collect as many matching pathnames as possible before each exec; in lieu of invoking a program with just a single pathname argument, it's invoked with many.

In your case the issue was semantics not performance, but when dealing with a large number of pathnames, exec-+ will yield a substantial performance gain over exec-; (a lot less fork/exec work to do).

For the detailed explanation, see the find(1) man page under the -exec section: Man Page for find (POSIX Section 1) - The UNIX and Linux Forums

Regards,
Alister