Get the number of files changed last month

I am trying to calculate how many PDF files were created last month. I use the following command but returns 0

 find . -name '*.pdf' -mtime +46 ! -mtime +30 | wc -l 

I am in the correct directory and it seems that the logic is correct ... any ideas on why this is not working? Is there an easier way, say, to convey the specific month that I am looking for instead of trying to calculate days like this?

+6
source share
3 answers

You will find all pdf files:

  • 46 days ago
  • not 30 days ago

     x>46 && x<=30 --> false 

It will return an empty result.


  Numeric arguments can be specified as +n for greater than n, -n for less than n, n for exactly n. 

If you want to find all pdf files ( 30<x<46 ):

 $ find . -name '*.pdf' -mtime +30 -mtime -46 
+21
source

If you use GNU find , you can specify absolute dates as follows:

 find . -name '*.pdf' -newermt 2012-01-31 ! -newermt 2012-02-29 | wc -l 

The -newermt option will find files that have been changed more recently than absolute time.

If you are not using GNU, you can use touch to create two files with the appropriate timestamps and search for your PDF files as follows:

 touch -t 201201312359 oldest # 11:59 PM 1/31/2012 touch -t 201203010000 newest # midnight 3/1/2012 find . -name '*.pdf' -newer oldest ! -newer newest | wc -l 

See the GNU documentation for more details.

+8
source

It seems you are looking for files older than 46 days, but not older (i.e. younger) than 30 days.

How about this?

  find . -name '*.pdf' -mtime -46 -mtime +30 
+6
source

Source: https://habr.com/ru/post/910906/


All Articles