UNIX / BASH: Listing files modified in specific month
date
allows you to easily generate timestamps for purposes like that:
date -d "01-Mar-2011 -1 sec" # last second of Feb-2011
Fortunately, the same syntax is possible in find
:
month="Mar-2010"find . -newermt "01-$month -1 sec" -and -not -newermt "01-$month +1 month -1 sec"
will find all files modified in March 2010
Well, I can create files that have the minimum timestamp and the maximum timestamp in February, and files that are just beyond February in each direction.
$ touch -t 201102010000.01 from$ touch -t 201102282359.59 to$ touch -t 201103010000.01 march$ touch -t 201101312359.59 january$ ls -ltotal 0-rw-r--r-- 1 mike None 0 Feb 1 00:00 from-rw-r--r-- 1 mike None 0 Jan 31 23:59 january-rw-r--r-- 1 mike None 0 Mar 1 00:00 march-rw-r--r-- 1 mike None 0 Feb 28 23:59 to
Then using GNU 'find' like this seems to show just the files whose timestamp is in February.
$ find -newermt '2011-02-01' ! -newermt '2011-03-01' -print./from./to
I don't know how portable these arguments are to other versions of 'find'.
Adding to Pumbaa80's answer:
In my pre-production environment, find does not support -newermt.
What I did instead was:
- Get a list of all possible files (via
find
,ls
etc.) Generate the timestamps of the last second of last month and this month
LAST_MONTH=$(date -d "01-Jun-2015" -1 sec +%s)THIS_MONTH=$(date -d "31-Jul-2015" +%s)
Iterate over the list from point 1 and compare the timestamp of each file with the timestamps from point 2
for file in $LIST_OF_FILESdo TIMESTAMP=$(stat -c"%Y" $file) if (( $LAST_MONTH < $TIMESTAMP )) then if (( $TIMESTAMP < $THIS_MONTH )) then echo "Your code here" fi fidone