How to find all files other than last two dates per month and year?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find all files other than last two dates per month and year?
# 1  
Old 04-28-2014
How to find all files other than last two dates per month and year?

Hi All,
lets say there are following files in directory
Code:
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 a.txt
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 b.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 2012 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 e.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 eio.txt
-rwxr-xr-x   1 user  userg         1596 Mar 27 2012 ee.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 f.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 g.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 2012 h.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 2012 i.txt
-rwxr-xr-x   1 user  userg         1596 Feb 28 2012 j.txt
-rwxr-xr-x   1 user  userg         1596 Feb 28 2012 jj.txt
-rwxr-xr-x   1 user  userg         1596 Apr 02 2013 k.txt
-rwxr-xr-x   1 user  userg         1596 Apr 11 2013 l.txt
-rwxr-xr-x   1 user  userg         1596 Apr 11 2013 m.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2013 n.txt
-rwxr-xr-x   1 user  userg         1596 Apr 27 2013 o.txt
-rwxr-xr-x   1 user  userg         1596 Apr 29 2013 oo.txt

for Mar 2012
last two dates are Mar25 & Mar27

for Feb 2012
last two dates are Feb28 & Feb23

for Apr 2012
last two dates are Apr27 & Apr29

So i want all files in directory other than above dates which are in red colour

output should be:
Code:
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 a.txt
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 b.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 2012 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 f.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 g.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 2012 h.txt
-rwxr-xr-x   1 user  userg         1596 Apr 02 2013 k.txt
-rwxr-xr-x   1 user  userg         1596 Apr 11 2013 l.txt
-rwxr-xr-x   1 user  userg         1596 Apr 11 2013 m.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2013 n.txt

---------- Post updated at 10:59 AM ---------- Previous update was at 09:02 AM ----------

guys Any help??

Last edited by Makarand Dodmis; 04-28-2014 at 12:58 PM..
# 2  
Old 04-28-2014
Code:
ls -l | egrep -v "Mar 2[57]|Feb 2[38]|Apr 2[79]"

Did not test this.
# 3  
Old 04-29-2014
I dont want hardcoded .. there can be any month any year in the directory...
# 4  
Old 04-29-2014
You have asked several similar questions and been given code that solved those problems. Based on what you learned from those previous examples, what have you tried?

We are happy to help you learn how to use the tools that are available on UNIX and Linux systems, but we are not here to act as your unpaid programming staff.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-29-2014
My 2 cents:
Since you have modified so many times the request, now it is clear to me that the solutions will all be of some complexity, I would 23 years ago solve this writing a cobol program (dont laugh I did a french conjugaison program in cobol for my diploma, the biggest part was algorithmics using only 2 terminaison arrays for all verbs finishing in -er ...).
Oracle taught me to always start with the table is less records..
So
One way of speeding up the whole data processing would be to split the tasks into smaller ones: making lists of files per month by year then processing these lists could be parallelised.
The list treatment:
date sort first and read the last file's date then compare with its previous till date changes saving files names - at date change start again (saving the files names) then stop at next date change
You have now all the files concerned by last 2 dates of month: a grep -v of this new list will give the files you want
You repeat that for all the month lists you have
# 6  
Old 04-29-2014
till now i only get expected result by below script
Code:
nawk '$6!=m{m=$6; c=0} {if($7!=d){if(c++>n)print b p; b=x} else b=b p ORS} {p=$0; d=$7}' n=2

this was created by Scritinizer but frankly speaking i am not able to reuse it for this thread.

I also currently i have solution for this thread but it is taking 40-45 mins and it includes for loops.

So if i get any better solution then it would be good.
# 7  
Old 04-29-2014
I saw your solution, and it takes time because comparing date over all the files...
I dont know what box is running this but using month lists can speed quite alot because less files, and can do more than one month at a time...Because you dont want the last 2 files of a month but the 2 latest dates of files you will have to find the last and compare, but once you got the date, find also the files of that date...
I did write a script for you last before Easter, but went on vacation and someone removed it...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg ... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

2. Shell Programming and Scripting

How to find all files for same month and year?

Hi All, I find all files for same month and year lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 2012 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 f.txt -rwxr-xr-x 1... (8 Replies)
Discussion started by: Makarand Dodmis
8 Replies

3. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

4. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

5. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

6. UNIX for Dummies Questions & Answers

Unix man command to find out month of the year?

how can i display month of the year i was born with using man command? thanks (2 Replies)
Discussion started by: janetroop95
2 Replies

7. Shell Programming and Scripting

Concatenating Files In A Year/Month/Day File Structure

Hi Im trying to concatenate a specific file from each day in a year/month/day folder structure using Bash or equivalent. The file structure ends up like this: 2009/01/01/products 2009/01/02/products .... 2009/12/31/products The file I need is in products everyday and I need the script to... (3 Replies)
Discussion started by: Grizzly
3 Replies

8. UNIX for Advanced & Expert Users

how to get the last month and year in UNIX

how to get the last month and year in UNIx (2 Replies)
Discussion started by: Vijay06
2 Replies

9. UNIX for Advanced & Expert Users

find files with a perticular year of access

Hello all, Might be a silly question, on my AIX machine the year had changed to 2022 and some files were accessed on this date hence the time stamp on these files is with year 2022, there are many such files. i want to list all these file from the root dir and subdir with 2022 year... (3 Replies)
Discussion started by: pradeepmacha
3 Replies
Login or Register to Ask a Question