开发者

Listing files modified in specific month

I am struggling with listing files modified in a specific month (for example, in February).

Here are several unsuccessful attempts:

  1. I tried creating temporary files and setting their timestamp to the first time in the next month and the first time in the target month and use -newer in find, like this:
    find -newer "$from" ! -newer "$to"

This lists files modified in the time int开发者_高级运维erval ($from, $to], but I would like the time interval [$from, $to) (otherwise, there would be false positives on files created on the first second in the next month). Listing files modified in February is another problem, since this would require to set one of the timestamps to the greatest one still in February, but the number of days in February varies depending on whether it is a leap year or not, which requires extra checking.

  1. If I use ls, I encounter a lot of complication when parsing, because of the possibility that user names or groups contain whitespace.

Is there an easy way and relatively portable way for doing this (so it works for any month, regardless of file names, etc.)?


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.

See the option -newerXY in find's man page.


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 -l
total 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:

  1. Get a list of all possible files (via find, ls etc.)
  2. 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)
    
  3. 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_FILES
    do
        TIMESTAMP=$(stat -c"%Y" $file)
        if (( $LAST_MONTH < $TIMESTAMP ))
        then
            if (( $TIMESTAMP < $THIS_MONTH ))
            then
            echo "Your code here"
            fi
        fi
    done
    


A workaround could be to use the -printf option to find:

find -printf "%Cm %p\\n"| egrep ^02 |cut -b4-

I don't think find can filter the -printf result itself, nor can it filter on date elements.

edit or if you really want the ls-like output:

find -printf "%Cm " -ls | egrep ^02 |cut -b4-
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜