Sorting the files on their timestamps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting the files on their timestamps
# 1  
Old 11-30-2011
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
Code:
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.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-30-2011 at 08:27 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-30-2011
Are your looking for the specific flag in ls?

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

# 3  
Old 11-30-2011
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
# 4  
Old 11-30-2011
Hi,

Thanks for your replies.

@Anchal, -lrt will not work as the following command processes one file at a time returned from find:
Code:
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.

Last edited by Franklin52; 11-30-2011 at 08:48 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 11-30-2011
Quote:
Originally Posted by bzk
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.
You don't need to pipe to cut and sort! sort's "-t" option does that for you:
Code:
# 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:
Code:
find . -name "*_20111101.gz" -type f | xargs -I {} ls -lrt "{}"

# 6  
Old 11-30-2011
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:
Code:
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.:
Code:
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

Last edited by rbatte1; 11-30-2011 at 08:54 AM.. Reason: Spelling
# 7  
Old 11-30-2011
Hi Robin,

Thanks a lot, it worked fine with
Code:
find DIR -name "*_20111101.gz" -print | xargs ls -lrt

Thanks all for your quick responses.

Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 11-30-2011 at 11:32 AM.. Reason: code tags, see PM
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching timestamps in two files.

OK. if the first file is : 3184 2014-07-28 04:15 global.Remote-Access 10.111.8.25 81.245.6.25 tcp 3268 3035 2014-07-28 04:16 global.Remote-Access 10.111.8.12 81.245.6.25 tcp 3268If the second file is: 1 Jul 28 04:12 2014-07-28 id967254(BGC-TLW-Cert) Tunneling: User with IP... (8 Replies)
Discussion started by: fxsme
8 Replies

2. Shell Programming and Scripting

Issues with comparing 2 files timestamps

Hi, Im trying to write a script to first check a directory and if the filename has "ACK" in it do nothing and exit but if it has "ORD" in the filename then compare it with a dummy file created 2 minutes previous and see which one is newer Im getting a few errors which im unsure how to rectofy... (5 Replies)
Discussion started by: 02JayJay02
5 Replies

3. Shell Programming and Scripting

Complex data sorting in excel files or text files

Dear all, I have a complex data file shown below,,,,, A_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 C_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 A_ABCDEF715 52410.9 18598.2 10611 10754.7 122535 252426 36631.4 C_DBCDI_1353 0... (19 Replies)
Discussion started by: AAWT
19 Replies

4. Shell Programming and Scripting

Sorting files

I have the following set of files and I want to order them according to the ascending values of the run: For example, doing ls -lrt *drw*.log gives n02-z30-sr65-rgdt0p25-dc0p08-4x3drw.log n02-z30-sr65-rgdt0p25-dc0p03-8x6drw.log n02-z30-sr65-rgdt0p25-dc0p01-8x6drw.log ... (18 Replies)
Discussion started by: kristinu
18 Replies

5. Shell Programming and Scripting

Bash script to copy timestamps of multiple files

Hi, I have a bunch of media files in a directory that have been converted (from MTS to MOV format), so my directory contains something like this: clip1.mts clip1.mov clip2.mts clip2.mov The problem is that the .mov files that have been created have the timestamps of the conversion task,... (2 Replies)
Discussion started by: Krakus
2 Replies

6. UNIX for Dummies Questions & Answers

help with sorting files

find / -type f 2> /dev/null | find -inum +1 2> /dev/null | find -mtime -30 2> /dev/null what i am trying to do i search all regular files in root directory with one or more inodes modified within last 30 days. the /dev/null is to suppress the permission denied outputs. i am now trying to... (5 Replies)
Discussion started by: iluvsushi
5 Replies

7. Shell Programming and Scripting

Getting all the .gz files between Two Date/TimeStamps

Hi, I am trying to write a script to ftp and get all the files between two date/time stamps from a archive directory. I have sent an attatchment of my archive directory. With the script I intend to get files for ex: between request.log.2008-08-22-03-53-49.gz &... (3 Replies)
Discussion started by: openspark
3 Replies

8. Shell Programming and Scripting

Concatenate all the files in folder on timestamps

Hi Gurus, Experts, I am facing a problem to concatenate all the files based on timeStamps. The Problem is like this . The are many files in a folder Which are of the below types Ack_to_MDS_20070809141159.xml Ack_to_MDS_20070809141157.xml Ack_to_MDS_20070809141155.xml... (19 Replies)
Discussion started by: srikanthgr1
19 Replies

9. Shell Programming and Scripting

Sort files by Date-timestamps available in filename & pick the sortedfiles one by one

Hi, I am new to Unix shell scripting. Can you please help me with this immediate requirement to code.. The requirement is as given below. In a directory say Y, I have files like this. PP_100000_28062006_122731_746.dat PP_100000_28062006_122731_745.dat PP_100000_28062006_122734_745.dat... (4 Replies)
Discussion started by: Chindhu
4 Replies

10. Shell Programming and Scripting

Sorting files

Hi, What is the command for sorting files according to their size Thanx in advance (4 Replies)
Discussion started by: sendhil
4 Replies
Login or Register to Ask a Question