Linux Script: FilesByWeek

June 22, 2009

in Linux,Tools

FilesByWeek is a small script that counts the number of files in a folder that were created (or last modified) in X week of the year. It’s designed for use with Linux/Postfix Maildirs (and thus excludes the standard .Sent folder and any Dovecot/Courier IMAP files from the find query), but should work just fine on any kind of directory.

Get the latest version here. Or, if you just want to see what the fuzz is about:

#!/bin/bash
typeset -i YEAR WEEK COUNT
 
WEEK=$1
# Comment out the following line if the year starts on a Monday
WEEK=$((WEEK-1))
# Use current system year by default. This can be changed to e.g.: YEAR=2008
YEAR=`date +%Y`
TGTDIR=$2
COUNT=0
 
\find ${TGTDIR} \
-type d \( -name "*.sent" -o -name "*.Sent" -o -name "courierimapkeywords" -o -name "courierimaphieracl" \) -prune -o \
-type f \( ! -name "subscriptions" ! -name "courierimapsubscribed" ! -name "dovecot.index.log*" ! -name "dovecot.index" ! -name "maildirfolder" ! -name "dovecot-keywords" ! -name "dovecot.index.cache" ! -name "courierimapacl" ! -name "courierimapuiddb" ! -name "dovecot-uidlist" \) \
-print |
{
    while read FILENAME; do
        if [[ `\date +%Y-%W -r "${FILENAME}"` == ${YEAR}-${WEEK} ]]; then
            # Uncomment to show the names of matching files
            # echo ${FILENAME}
            let COUNT++
        fi
    done
 
    echo Week $1 -- ${TGTDIR}: ${COUNT}
}
 
exit 0
Related Posts with Thumbnails

{ 1 comment… read it below or add one }

1 Charles W. July 11, 2009 at 17:20

Thanks for this! We need to do weekly mail stats for some of our IMAP accounts and I hadn’t thought of doing it this way. A few modifications to this and I think we’re set! :-)

Reply

Leave a Comment