How to find last two files for a month?

Hi All,
I need to find last two files for the month.
lets say there are following files in directory

-rwxr-xr-x   1 user  userg         1596 Mar 19 15:43 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 21 15:43 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 15:43 f.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 15:43 h.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 15:43 w.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 15:43 q.txt
-rwxr-xr-x   1 user  userg         1596 Feb 21 15:43 a.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 15:43 s.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 15:43 k.txt
-rwxr-xr-x   1 user  userg         1596 Feb 27 15:43 j.txt

output should be:

Mar 24 15:43 h.txt
Mar 25 15:43 w.txt
Feb 23 15:43 k.txt
Feb 27 15:43 j.txt

You would need to repeat this monthwise:

 ls -lt | grep Mar | head -2
1 Like
ls -lrt | awk '{a[(NR % 2) $6] = ($6 FS $7 FS $8 FS $9)} END {for(x in a) print a[x]}'
1 Like

Try :

$ find . -name '*.txt' -mtime -60 -mtime +10 -ls

-60 60 days old but

+10 days younger since today is April 10

10< query <60

Thanks a lot Srini & Rudic
Actualy i want all files other than last two files per month.
can it be posibble?

-rwxr-xr-x   1 user  userg         1596 Mar 19 15:43 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 21 15:43 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 15:43 f.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 15:43 h.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 15:43 w.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 15:43 q.txt
-rwxr-xr-x   1 user  userg         1596 Feb 21 15:43 a.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 15:43 s.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 15:43 k.txt
-rwxr-xr-x   1 user  userg         1596 Feb 27 15:43 j.txt

output should be:

c.txtd.txtf.txtq.txta.txts.txt

---------- Post updated at 09:35 AM ---------- Previous update was at 09:32 AM ----------

-rwxr-xr-x   1 user  userg         1596 Mar 19 15:43 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 21 15:43 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 15:43 f.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 15:43 h.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 15:43 w.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 15:43 q.txt
-rwxr-xr-x   1 user  userg         1596 Feb 21 15:43 a.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 15:43 s.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 15:43 k.txt
-rwxr-xr-x   1 user  userg         1596 Feb 27 15:43 j.txt

output should be:

Code:
For Marc.txtd.txtf.txtfor Febq.txta.txts.txt

This is definitely unreadable!

Hi All,
I need to find all files other than last two files per month.
lets say there are following files in directory

Mar 19 15:43 c.txt
Mar 21 15:43 d.txt
Mar 22 15:43 f.txt
Mar 24 15:43 h.txt
Mar 25 15:43 w.txt
Feb 12 15:43 q.txt
Feb 21 15:43 a.txt
Feb 22 15:43 s.txt
Feb 23 15:43 k.txt
Feb 27 15:43 j.txt

output should be:

Mar 19 15:43 c.txt
Mar 21 15:43 d.txt
Mar 22 15:43 f.txt
Feb 12 15:43 q.txt
Feb 21 15:43 a.txt
Feb 22 15:43 s.txt

Try

ls -lt | grep Mar | tail -n+3

this is giving error
usage: tail [+/-[n][lbc][f]] [file]
tail [+/-[n][l][r|f]] [file]

man tail :

for tail (GNU coreutils) 8.20
So -n+3 gives you all lines except for the first two... Adapt for your tail version.

Sorry ,

I am using unix solaris 5.8 ksh

Using a small circular buffer, files from multiple months (and years) can be handled in a single pass:

ls -lrt | awk 'm!=$6 {m=$6; i=0} ++i>2 {print a[i%2]} {a[i%2]=$0}'

Since the date format is locale dependent, you may want any ls-based solution to explicitly set the locale.

I barely tested it, and then only in my locale.

Regards,
Alister

Hi Alister,

Result by your code is partialy correct.

i want files not to disaply of first two file dates & last file date for a month.

Suppose there are 4 files for 1 Apr 2014 date & 4 files 2 Apr 2014 & 2 files 30 Apr 2014 then these all 10 files should not get displayed.

Regards,
Dodmis

Post #1 states that you only want to keep the last two files of each month.

Posts #5 and #7 indicate the inverse: the last two files of each month should be discarded.

Now, with post #13, you present a third permutation which appears to require keeping all files except those whose dates match the first 2 days of the month and the last day of the month.

If you want volunteers to help you freely, the least you can do is present a single, coherent problem statement. Speaking for myself, you're not paying me enough to track this moving target.

Regards,
Alister

Appologies Alister.I am very new to this forum.Please bare with me.

My final problem statement as follows:

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 2012 q.txt
Feb 21 2012 a.txt
Feb 21 2012 aa.txt
Feb 22 2012 s.txt
Feb 23 2012 k.txt
Feb 27 2012 j.txt
 
Mar 19 15:43 c.txt
Mar 21 15:43 d.txt
Mar 22 15:43 f.txt
Mar 24 15:43 h.txt
Mar 25 15:43 w.txt
Mar 25 15:43 ww.txt
 
Feb 12 15:43 q.txt
Feb 21 15:43 a.txt
Feb 22 15:43 s.txt
Feb 23 15:43 k.txt
Feb 27 15:43 j.txt
Feb 27 15:43 jj.txt

output should be:

#for Mar 2012
Mar 22 2012 f.txt
Mar 24 2012 h.txt
 
#for Feb 2012
Feb 22 2012 s.txt
Feb 23 2012 k.txt
 
#for Mar 2014
Mar 22 15:43 f.txt
Mar 24 15:43 h.txt
 
#for Feb 2014
Feb 22 15:43 s.txt
Feb 23 15:43 k.txt