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?
# 15  
Old 05-18-2011
Quote:
ls -lrt | awk '{print $6$7" "$9 }' | grep `date +%b""%d`
This suggested command contains design and implementation errors. Consider what happens on April 1st. The grep match string becomes "Apr01" which will not match a directory list.


A less-than-perfect way of picking up files and directories created today is to match against the date and check that there is a colon character somewhere on the line.
Code:
TODAY="`date '+%b %e'`"        # Mmm d
ls -lar | grep "${TODAY}"|grep ":"

The importance here is to get the "date" command right and to preserve spaces in the date throughout by proper use of double quote characters.

Beware of dates with a single digit day. The command "date +%d" which is wrongly advised in various earlier posts gives the day of the month with a leading zero but "ls -lar" give the day of the month with a leading space. Therefore we must use "date +%e" and preserve that space character.
My example is not a recommended method (mainly because it does not work with filenames containing colon characters and is awkward with filenames containing space characters). My earlier post in this thread is a recommended method.
# 16  
Old 05-18-2011
Dear experts,

Do you find find Smilie with touch to be much more reliable in scripts ?
# 17  
Old 05-18-2011
If you are referring to post #1 , yes. This is mainly because the format of "ls -la" changes for older files (the time gets replaced with the year).
Quote:
I have a few log files which get generated on a daily basis. So, I need to pick only the ones which get generated for that particular day.
There are more techniques available depending on exactly what Operating System and tools you have.
For the moment we'll assume that you don't let an open log run through midnight. This can make scripting "on a daily basis" difficult.
# 18  
Old 05-19-2011
Methyl the code you provided picks the files created on a particular day ie today and lists them on the screen, but my question is, after you run this script, lets say i have 2 files created on the same day, How do i copy these 2 files to a different location..if this is answered then i believe i can close this discussion for good..Please help..Thanks
win4luv
# 19  
Old 05-19-2011
Well, you cannot hide ' inside ', and you have to do a 'command stdout to string' conversion $(...) or `...`. Think of echo as 'command line string to stdout' converter, and this is the opposite.
Code:
cp $(
 ls -lrt | awk '
   {print $6$7" "$9 }
  ' | grep `date +%b""%d` | awk '
   {print $2}
  ' ) somewhere/new

Now, if you dislike nesting:
Code:
 ls -lrt | awk '
   {print $6$7" "$9 }
  ' | grep `date +%b""%d` | awk '
   {print $2}
  ' | mv $( cat ) somewhere/new

Both of these are ugly if no files, so sometimes, put the list in an env. var.:
Code:
new=$(
 ls -lrt | awk '
   {print $6$7" "$9 }
  ' | grep `date +%b""%d` | awk '
   {print $2}
  '
 )
 
if [ "$new" != "" ]
then
 mv $new somewhere/new
fi

If the number of files is really big, like from a long find, then waiting for a complete list is an unconscionable delay, so you want to pipeline parallel more:
Code:
ls -lrt | awk '
   {print $6$7" "$9 }
  ' | grep `date +%b""%d` | awk '
   {print $2}
  ' | while read f
 do
  mv $f somewhere/new
 done

but this lacks economy of scale on mv calls (nice for must-do-one-at-a-time commands), so use xargs to batch them up:
Code:
ls -lrt | awk '
   {print $6$7" "$9 }
  ' | grep `date +%b""%d` | awk '
   {print $2}
  ' | xargs -n101 echo | while read fs
 do
  mv $fs somewhere/new
 done

xargs is 'pile args from this many stdin lines on the end of the supplied command and execute'. The number 101 is magic, in that you can argue that you got over 99% of any economy of scale while minimizing latency to first mv. Specifying the number to xargs with -n not only limits the delay to first mv, it also says no execute for 0 lines.

The grep pattern needs to be `date '+^%b''%d '` so it both matches the date only at beginning of line (^) not in the file name, and matches the space separator (May1 and not May19 ).

Last edited by DGPickett; 05-19-2011 at 03:36 PM..
# 20  
Old 05-19-2011
This is just about perfect. Awesome piece of work Pickett..Thanks a ton Smilie..You are the Man
win4luv
# 21  
Old 05-19-2011
Good tricks to use for lots of stuff! Enjoy!
 
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