Sorting the files on their timestamps

Hi,

The file names are like XXXX_20111101.gz for 01 November 2011, etc. There might be several files for 01 November 2011 and I can find them using

find DIR -name *_20111101.gz  -exec ls -lt {} \;

But I want to sort the listing returned by the above command on the timestamps of the files ascending.

Can anyone please provide inputs? Thanks in advance.

Are your looking for the specific flag in ls?

find DIR -name *_20111101.gz -exec ls -lrt {} \;

If ls -latr won't satisfy you, you may try to pipe output from find, "cut" the word between "_" and ".", and then just "sort" it.

--j

Hi,

Thanks for your replies.

@Anchal, -lrt will not work as the following command processes one file at a time returned from find:

find DIR -name *_20111101.gz -exec ls -lrt {} \;

@bzk, cut will not work as I wanted the files for the same day sorted out as per their timestamps. Every file here will have _20111101. in its name.

You don't need to pipe to cut and sort! sort's "-t" option does that for you:

# find . -name "*20111101*"
./1_20111101_2.txt
./1_20111101_1.txt
./1_20111101_4.txt
./1_20111101_3.txt
# find . -name "*20111101*" | sort -t "_" -k 3n
./1_20111101_1.txt
./1_20111101_2.txt
./1_20111101_3.txt
./1_20111101_4.txt

I hope it helps!

---------- Post updated at 10:45 ---------- Previous update was at 10:42 ----------

Try this one:

find . -name "*_20111101.gz" -type f | xargs -I {} ls -lrt "{}"

What you curently have will issue ls -lt for each file individually as it is listed by find, so all you will get is the list in whatever order find gives them to you. Using ls -lrt will probably have no effect in what you already have.

Could I suggest:

find DIR -name "*_20111101.gz" -print | xargs ls -lrt

Note the quotes as otherwise the * will get expanded and may blow your command length limit. You might even find that "more than one" file listed in this way will be invalid to find anyway.

This will find all the files and then pass that list through xargs to an ls -lrt. It's not perfect as there is a limit to xargs, but you will have to see you man pages for that and also experiment a little.

Are we to assume that the XXXX is a time? If so, could that be forced to the end of the filename? If so, then, chronologically speaking, the most significant digits are first, i.e. CCYYMMYY_HHmm.gz ...If you can, then it is a simple sort based on the filename, e.g.:

ls -l 20111101_*.gz|sort -n

...will give the largest number/newest file last.

I hope that these suggestions help, but please feel free to write again if I have missed the point completely.

Robin,
Liverpool/Blackburn
UK

Hi Robin,

Thanks a lot, it worked fine with

find DIR -name "*_20111101.gz" -print | xargs ls -lrt

Thanks all for your quick responses.