ls -lt on find command

Hi Guys...I would like to use find command to search certain type of files and then ls -lt in time order.

find . -name "test.txt" -exec ls -lt {} \;

or

find . -name "bi_job_control.console" -exec ls -lt {} \; | sort -k6,7

does not work as that does not give a true time line about when these files were modified. Is there an easier way?The workaround I have found is

for f in `find . -name "test.txt"`> do> str=$str" "$f> done

i.e. create the string and then us

ls -ltls -lt $f

But I am hoping for a more cleaner , efficient way.Any idea guys?Regards,R.

---------- Post updated at 01:14 PM ---------- Previous update was at 10:38 AM ----------

Thanks. From next time I will be using the code tags.

see the -mtime and -atime options in find command.
Refer the Link : UNIX man pages : find ()

find command with ls -lt will only list with time sort on that single file, not all files.

If your system's command line length limit is sufficient to accomodate the entire file list, you can use:

find . -name "test.txt" -exec ls -lt {} +

Regards,
Alister

1 Like

Thanks for your answer Alister and in fact all who contributed. Now looking at man find makes more sense :slight_smile:

@alister

This does not work. It does not sort the files by timestamp.

@rahulkav
A much shorter version of your solution. We can generate the files list directly. Solution only works if you don't exceed the maximum length of the "ls" command line and if none of the directory or file names found contain space characters.

ls -lt `find . -type f -name "test.txt" -print`
Or
ls -lt $( find . -type f -name "test.txt" -print )

Hello, methyl:

Odd. Works fine here. My find-exec-+ and your ls-command substitution suggestions should be practically identical, except in cases where the list of files includes members with IFS characters (the command substitution will break) or where the list exceeds the command line length limit (yours would error out and mine would exec multiple ls commands (undesirably yielding two independently sorted lists)).

Regards,
Alister

@Alister

I guess he missed the "+" tricky stuff, haha i was also ignoring that stuff, i will sleep a little less ignorant tonight :smiley: