How to pick only the latest files based on the timestamp?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to pick only the latest files based on the timestamp?
# 36  
Old 06-06-2011
Code:
ls -lrt | awk ' {print $6$7" "$9 }' |grep `date '+%b''%d' |sed 's/0\([0-9]\)$/\1/'` | awk ' {print $2} '

# 37  
Old 06-06-2011
Code:
ls -lrt | nawk -v D="$(date +'%b%e:'| sed 's/ //g')" 'D==$6$7":"{print $NF}'

Code:
# ls -lrt
[...]
-rw-r--r--   1 admin    admin         30 Jun  3 21:48 f2
-rw-r--r--   1 admin    admin         12 Jun  3 21:48 f3
-rw-r--r--   1 admin    admin          4 Jun  3 22:39 f4
-rw-r--r--   1 admin    admin       1810 Jun  4 00:08 bigone
-rw-r--r--   1 admin    admin       1568 Jun  6 14:22 tst
-rw-r--r--   1 admin    admin          0 Jun  6 21:35 toto
-rw-r--r--   1 admin    admin          0 Jun  6 21:35 titi
# date
Mon Jun  6 21:36:00 CEST 2011
# date +'%b%e:'| sed 's/ //g'
Jun6:
# ls -lrt | nawk -v D="$(date +'%b%e:'| sed 's/ //g')" 'D==$6$7":"{print $NF}'
tst
toto
titi
#

I added the colon : stuff just in the case you want to match files from let's say for example :

the Jun 1 without matching those from the Jun 10

Last edited by ctsgnb; 06-06-2011 at 04:44 PM..
# 38  
Old 06-06-2011
touch 06060000 /tmp/mark.$$ ; find * -type f -newer /tmp/mark.$$
# 39  
Old 06-06-2011
... to secure a bit the output in the case some of your files contains space in their name :
Code:
ls -lrt | nawk -v D="$(date +'%b%e:'| sed 's/ //g')" 'D==$6$7":"{sub(".*"$9,$9);print}'

# 40  
Old 06-06-2011
the above codes mentioned works fine but i would like to know if the script would work fine for double digit dates as well(10-30/31)....


I added the colon : stuff just in the case you want to match files from let's say for example :

the Jun 1 without matching those from the Jun 10


Dint understand what you meant..could u pls elaborate more on this?


Thanks to both of you
win4luv
# 41  
Old 06-07-2011
What i meant is when you
Code:
ls -lrt | awk '{print $1$2,$9}'

imagine you have some file from 1 and 10 of june,

if you simply grep "Jun1"

you will get both
Jun1 and Jun10 since "Jun1" is a subpattern contained in Jun10
instead of only getting file from the date of Jun1 :

Code:
# echo "Jun  1\nJun 10" | awk '{print $1$2}' | grep 'Jun1'
Jun1
Jun10
# echo "Jun  1\nJun 10" | awk '{print $1$2":"}' | grep 'Jun1:'
Jun1:
# echo "Jun  1\nJun 10" | awk '{print $1$2":"}' | grep 'Jun10:'
Jun10:

# 42  
Old 06-07-2011
Yes, whenever grepping a variable area for an exact value, you need a separator to the right to avoid matching prefixes, and to the left to avoid matching suffixes, but Jun fif that. If you are not anchored to the trailing separator, and if the trailing separator and string can occur in the 'payload' as well as the 'key', you need to anchor your regex to one end of the line. I guess your file names are 'well behaved', but life is not always so.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with UNIX command to get the latest file from list of files with timestamp

Hi All, I have list of files like below with name abcxyz.timestamp. I need a unix command to pick the latest file from the list of below files. Here in this case the lates file is abcxyz.20190304103200. I have used this unix command "ls abcxyz*|tail -1" but i heard that it is not the appropriate... (2 Replies)
Discussion started by: rakeshp
2 Replies

2. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

3. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

4. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

5. Shell Programming and Scripting

Urgent ...pls Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (1 Reply)
Discussion started by: robertbrown624
1 Replies

6. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

7. UNIX for Dummies Questions & Answers

Need command to pick the latest file

Hi In my script i am trying to access mainframe server using FTP, in the server i have filee with the timestamp.I need to get the file with the latest timestamp among them . The server has the below files / ftp> cd /outbox 250 CWD command successful ftp> ls 200 PORT command successful... (4 Replies)
Discussion started by: laxmi131
4 Replies

8. Shell Programming and Scripting

Find the latest directory and loop through the files and pick the error messages

Hi, I am new to unix and shell scripting,can anybody help me in sctipting a requirement. my requirement is to get the latest directory the name of the directory will be like CSB.monthdate_time stamp like CSB.Sep29_11:16 and CSB.Oct01_16:21. i need to pick the latest directory. in the... (15 Replies)
Discussion started by: sudhir_83k
15 Replies

9. UNIX for Dummies Questions & Answers

pick the very latest directory

Hi, I have some list of directories in the form datemonthyear e.g. 02082009, 03082009 and 04082009 etc. I need to pick the latest directory from the current working directory. Outcome: 05082009 This is the output am expecting. Thanks (6 Replies)
Discussion started by: venkatesht
6 Replies

10. Shell Programming and Scripting

Pick the latest set of files

I have task in which I need to pickup a set of files from a directory depending on the following criteria: Every month 6 files are expected to arrive at /test. The files come with date timestamp and the latest file set for the month needs to be used Suppose this is the set of files that present... (5 Replies)
Discussion started by: w020637
5 Replies
Login or Register to Ask a Question