Skip to content
Advertisement

Linux Grouping and Counting Files by attribute

I am trying to return a list of the months that files were created using the following code.

ls -l|awk '{A[$6":"]++}END{for (i in A){print i" "A[i]}}'

I am using the below code to validate each output.

ls -la | grep -c "Jan"

However as you can see from my output:

: 1
Jan: 19
Feb: 11
Mar: 28
Apr: 10
May: 14
Jun: 24
Jul: 4
Aug: 16
Sep: 10
Oct: 30
Nov: 4
Dec: 1

Output of ls|grep

I end up with 1 record showing no date. Also both January and December are short by 1. Can anyone assist?

Advertisement

Answer

You could do it this way using awk and sort

$ ls -l | awk '$6!=""{m[$6]++}END{for(i in m){printf "%s : %s%s",i,m[i],ORS }}' | sort -k1M
Jan : 7
Mar : 1
Apr : 8
Aug : 2

The problem comes with the first line of ls -l which doesn’t contain a month field

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement