11

I can find number of all files in folder but I got pretty large number.

find . -type f | wc -l      #find number of files in DIR
ls -lrt                     #list all files order by date  

How to find number of files par day?

So, the result should be something like:

# left number is number of files and right is one day.

109294 2016-06-27
101555 2016-06-26
88123  2016-06-25 
... etc. 
2
  • And what's the numbers on the left ? 109294 and 109294 Jun 27 '16 at 8:43
  • 1
    Sry, That is the number of files per day.
    – tasmaniski
    Jun 27 '16 at 8:45
22

You can do this using the printf action of find to print only the modification times in desired format, and then using sort and uniq:

find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c
  • -printf '%TY-%Tm-%Td\n' prints the modification time of files in e.g. 2015-05-23 format

  • sort sorts the output and uniq -c does the count by date

Example:

~/foobar% find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c
      3 2004-06-29
      1 2004-08-23
      1 2004-09-15
      1 2004-09-18
      1 2005-07-24
      1 2006-02-05
      2 2008-06-25
      3 2008-12-31
      1 2009-03-13
      1 2009-04-30
      1 2010-04-04
      2 2010-09-01
      8 2011-07-13
     15 2011-08-27
      3 2011-11-03
      3 2014-10-08
2
  • 2
    NB: these can be viewed graphically with Gnuplot using find . -type f -printf '%TY-%Tm-%Td\n' 2</dev/null | sort | uniq -c | tail -n +2 | gnuplot -p -e "set xdata time; set timefmt \"%Y-%m-%d\";set xtics rotate; plot '-' using 2:1 with impulses" Jun 27 '16 at 14:07
  • Interesting way, but it doesn't work. I got an graphic but with no data...
    – tasmaniski
    Jun 28 '16 at 14:24
4

Here's a solution with find + awk

find . -maxdepth 1 -type f -printf '%TY-%Tm-%Td\n' | awk '{array[$0]+=1}END{ for(val in array) print val" "array[val]   }'

Essentially what happens is that we find all regular files and print their modification time as specified by the %T format , and then awk takes over , and counts each line using associate arrays . the END{} statement uses for loop to go through all the elements in the associated array, and print key + array[key] contents ( which is the date + count ).

You may want to use sort to organize the output , particularly sort -k 1 based on column 1 (which is date), but that is optional. Also -maxdepth 1 will check for files only in the current folder. If you want to find files in subdirectories as well, remove -maxdepth 1 part.

Sample output

$ find . -maxdepth 1 -type f -printf '%TY-%Tm-%Td\n' | awk '{array[$0]+=1}END{ for(val in array) print val" "array[val]   }'

2015-09-29 1
2016-04-06 2
2016-04-07 10
2016-04-08 2
2015-11-05 2
2016-04-22 2
2016-04-23 6
2016-04-24 1
2015-11-21 2
2015-11-22 2
1
  • This might run faster than the |sort | uniq -c version, especially with very huge numbers of files and a small-ish set of different days. Collapsing down to counts in one step avoids sorting a large number of duplicates before counting. Jun 27 '16 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.