My requirement is to find the space consumed and count of incoming files every month. So say If I have a directory "X" I would like to know the space and for the month of November

Following is the command we used to get the details.

cd x
Output1=$(find . -type f -newermt 2017-11-01 ! -newermt 2017-11-30 | wc -l)
Output2=$(find . -type f -newermt 2017-11-01 ! -newermt 2017-11-30 | du -sk)
echo "Count of file is $Output1 and Space occupied by files is $Output2 KB"

We ran the above command for 3 different ranges a) 1 Oct to 31st Oct , b) 1st Oct to 30th Nov, and c) 1st Nov to 30th Nov.

My expectation was a) + b) should be = C but it is not. Can you please share your views on this. Or can you please let me know if there is anything wrong with the command I am using. Or Please share if you have a better option for my requirement.

Count of file is 3679280 and Space occupied by files is 19766351768 Count of file is 6857725 and Space occupied by files is 19765912668 Count of file is 3063226 and Space occupied by files is 19765541452


-newermt 2017-10-31 means "modified after October 31st, 00:00",
! -newermt 2017-10-31 means "modified before or on October 31st, 00:00" so the latter does not include files made during the day of October 31st. If you use -newermt 2017-10-01 ! -newermt 2017-10-31, you'll miss the last day of the month.

$ find . -type f -newermt 2017-10-01 ! -newermt 2017-10-31
$ find . -type f -newermt 2017-10-01 ! -newermt 2017-11-30
$ find . -type f -newermt 2017-11-01 ! -newermt 2017-11-30

You probably want -newermt 2017-10-01 ! -newermt 2017-11-01 to get the whole of October, but note that files created on exactly on midnight between Oct 31st and Nov 1st count for October. (This will probably not be an issue on any system with subsecond timestamps.)

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.