Group and count file by date

Hi all,
in BIN/SH I need to group and count files by date.

Ie:

ls -la
 
-rw-r--r--   1 aaa dba 122 Jul 13 14:28 as1.tmp
-rw-r--r--   1 aaa dba 122 Jul 13 15:27 as2.tmp
-rw-r--r--   1 aaa dba 122 Jul 21 17:04 as3.tmp
-rw-r--r--   1 aaa dba 122 Jul 23 15:45 as4.tmp
-rw-r--r--   1 aaa dba 122 Jul 23 17:26 as5.tmp
-rw-r--r--   1 aaa dba 122 Jul 27 18:39 as6.tmp
-rw-r--r--   1 aaa dba 122 Jul 28 10:53 as7.tmp
-rw-r--r--   1 aaa dba 122 Jul 28 18:07 as8.tmp
-rw-r--r--   1 aaa dba 122 Jul 29 16:54 as9.tmp
-rw-r--r--   1 aaa dba 122 Jul 30 16:33 as11.tmp
-rwxr-xr-x   1 aaa dba 122 Jan 12  2009 1*
-rwxr-xr-x   1 aaa dba 122 Jan 12  2009 2*
-rwxr-xr-x   1 aaa dba 122 Jan  8  2009 3*
-rwxr-xr-x   1 aaa dba 122 Jan  8  2008 4*
-rwxr-xr-x   1 aaa dba 122 Jan 21  2009 aaw.sh

Should be:

 8 Jan 2008 1
 8 Jan 2009 1
12 Jan 2009 2
21 Jan 2009 1
13 Jul 2009 2
21 Jul 2009 1
23 Jul 2009 2
27 Jul 2009 1
28 Jul 2009 2
29 Jul 2009 1
30 Jul 2009 1

Should be better this one:

20080108 1
20090108 1
20090112 2
20090121 1
20090613 2
20090621 1
20090623 2
20090627 1
20090628 2
20090629 1
20090630 1

Thanks a lot,
Riccardo

something like this will do i guess..

ls -l|awk '$8 ~ /..:../{gsub($8,"2009")}1'|awk '{A[$7" "$6" "$8]++}END{for (i in A){print i" "A}}'

vidyadhar85, I don't get the desired ouput with your solution....

ric79, try this:

ls -la |
awk ' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month]=i
}
$8 ~ /:/{$8="2009"}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a|"sort"}}'

Regards

Below command can give you the same result

stat -c "%y" * |cut -d' ' -f1|sort|uniq -c

will give you date like 2009-05-26

stat -c "%y" * |cut -d' ' -f1|sort|uniq -c|tr -d "-"

will give you date like 20090526

stat -c "%y" * .*|cut -d' ' -f1|sort|uniq -c|tr -d "-"

will include hidden files also

Regards,

Ranjith

ls -la |
awk ' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month]=i
}
$8 ~ /:/{$8="2009"}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a|"sort"}}'

1) Is it possibile to avoid "2009" and get the current year?
2) The script display also "0000 1". I need to count only Files and not Directories
3) Jan Feb Mar ... what does it happen if the unix version is Italian? Is there a way to make ls printing 01 instead of Jan?

Thanks,
Riccardo

Here I use a variabale for the year and the directories are ignored now with !/^-/{next}.
For another language you have to change the month names manualy.

ls -la |
awk -v year=$(date "+%Y")' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month]=i
}
!/^-/{next}
$8 ~ /:/{$8=year}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a|"sort"}}'

Regards

#!/bin/sh
ls -la |
awk -v year=$(date "+%Y") ' BEGIN{
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month, " ")
  for (i=1; i<=12; i++) mdigit[month]=i
  $mydate="2009"
}
!/^-/{next}
$8 ~ /:/{$8=year}
{
  dat=$8 sprintf("%02d",mdigit[$6]) sprintf("%02d",$7)
  a[dat]++
}
END{for(i in a){print i, a|"sort"}}'
 

gives me
syntax error at line 3: `(' unexpected

Works fine for me, try nawk or /usr/xpg4/bin/awk on Solaris.

Regards

Specify either '#!/bin/ksh' or '#!/bin/bash' instead of '#!/bin/sh'