{"id":56,"date":"2014-03-10T23:36:39","date_gmt":"2014-03-10T23:36:39","guid":{"rendered":"http:\/\/www.matthewmilcic.com\/blogs\/?p=56"},"modified":"2014-03-10T23:36:39","modified_gmt":"2014-03-10T23:36:39","slug":"bash-directory-cleaner","status":"publish","type":"post","link":"http:\/\/www.matthewmilcic.com\/blogs\/?p=56","title":{"rendered":"Bash Directory Cleaner"},"content":{"rendered":"<p>There are times when one will have non-rotating log files in one&#8217;s code. The scripts keep generating more logs based on the date and the directories keep filling up. What is one supposed to do?<\/p>\n<p>This is a quick Bash directory cleaner I use:<br \/>\n<code><br \/>\nfunction usage() {<br \/>\necho \"Usage: $0 [d directory] [t time_in_days]\"<br \/>\nexit 1<br \/>\n}<\/p>\n<p>TIME=30<br \/>\nHOMEDIR=\/home\/zenoss<br \/>\nSCRIPTSDIR=${HOMEDIR}\/scripts<br \/>\nLOGDIR=${SCRIPTSDIR}\/log<\/p>\n<p>REMOVEFILE=${LOGDIR}\/findResults.$$.txt<\/p>\n<p>while getopts d:t: flag<br \/>\ndo<br \/>\n    case \"${flag}\" in<br \/>\n        d) DIRECTORY=${OPTARG};;<br \/>\n        t) TIME=${OPTARG};;<br \/>\n        [?]) usage;;<br \/>\n    esac<br \/>\ndone<br \/>\nshift $((OPTIND - 1))<\/p>\n<p>if [ $# -ne 0 ]<br \/>\nthen<br \/>\n    usage<br \/>\nfi<\/p>\n<p>if [ ! -d \"$DIRECTORY\" ]<br \/>\nthen<br \/>\n    echo \"Directory specified was not a directory\"<br \/>\n    exit 2<br \/>\nfi<\/p>\n<p>find ${DIRECTORY} -mtime +${TIME} &gt; ${REMOVEFILE}<\/p>\n<p>echo \"Removing: \"<br \/>\ncat ${REMOVEFILE}<\/p>\n<p>for each in `cat ${REMOVEFILE}`<br \/>\ndo<br \/>\n    rm $each -f<br \/>\ndone<\/p>\n<p>rm ${REMOVEFILE}<br \/>\n<\/code><\/p>\n<p>This script allows one to chose a directory and how many days of logs to keep.  <\/p>\n<p>Here is an example usage:<\/p>\n<p><code><br \/>\n0 12 * * 1 \/home\/user\/scripts\/bin\/dirCleaner.sh -d\/home\/user\/scripts\/log -t31 >> \/home\/user\/scripts\/log\/dirCleaner.`date +\\%Y\\%m\\%d`.log 2>&1<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times when one will have non-rotating log files in one&#8217;s code. The scripts keep generating more logs based on the date and the directories keep filling up. What is one supposed to do? This is a quick Bash directory cleaner I use: function usage() { echo &#8220;Usage: $0 [d directory] [t time_in_days]&#8221; exit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[8],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-organization","category-technical","tag-bash"],"_links":{"self":[{"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":2,"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":58,"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions\/58"}],"wp:attachment":[{"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.matthewmilcic.com\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}