{"id":2180,"date":"2017-12-07T12:37:26","date_gmt":"2017-12-07T12:37:26","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2180"},"modified":"2020-01-09T09:45:38","modified_gmt":"2020-01-09T09:45:38","slug":"create-a-script-havent-been-accessed-for-a-week-then-delete-them","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/create-a-script-havent-been-accessed-for-a-week-then-delete-them\/","title":{"rendered":"Create a script haven\u2019t been accessed for a week, then delete them"},"content":{"rendered":"<p><strong>rajeshkumar created the topic: Create a script haven\u2019t been accessed for a week, then delete them<\/strong><br \/>\nCreate a script for a cronjob that checks a special directory for files with the extension .tmp that haven\u2019t been accessed for a week, then delete them. Also remove all empty directories.<\/p>\n<p><code>#!\/bin\/bash<\/p>\n<p>usage()<br \/>\n{<br \/>\necho \"Usage: $0 [-d Valid Directory] [-e Valid File extension] [-a Access time in a day]\" 1>&2;<br \/>\nexit 1;<br \/>\n}<\/p>\n<p>while getopts \":d:e:a:\" o; do<br \/>\n    case \"${o}\" in<br \/>\n        d)<br \/>\n\t    d=${OPTARG}<br \/>\n\t    if [ ! -d \"$d\" ]; then<br \/>\n\t    \techo \"Directory $d not found\"<br \/>\n\t    \tusage<br \/>\n\t\tfi<br \/>\n            ;;<br \/>\n        e)<br \/>\n\t    e=${OPTARG}<br \/>\n\t    echo \"Passed file extension is $e\"<br \/>\n            ;;<br \/>\n\ta)<br \/>\n\t    a=${OPTARG}<br \/>\n\t    echo \"Passed number of days for access is $a\"<br \/>\n            ;;<br \/>\n        *)<br \/>\n            usage<br \/>\n            ;;<br \/>\n    esac<br \/>\ndone<br \/>\nshift $((OPTIND-1))<\/p>\n<p># for Debug for output<br \/>\necho \"Directory is $d\"<br \/>\necho \"File extension is $e\"<br \/>\necho \"Accessed time is $a\"<\/p>\n<p># Based on Inputs, this will find the specified file and delete it found accessed  according to parameters<br \/>\n#find $d -iname \"*$e\" -atime -$a -type f -print0<br \/>\n#find \/root\/raj -iname \"*.txt\" -atime -1 -type f -print0<\/p>\n<p>find $d -iname \"*$e\" -atime -$a -type f -print0 | xargs -0 rm -rf<\/p>\n<p># This can be used as well but xargs would be faster for large file sets.<br \/>\n# find $d -iname \"*$e\" -atime -$e -type f | exec rm {} \\;<\/p>\n<p># To Remove the empty directory<br \/>\nfind $d -type d -empty -exec rmdir {} \\;<\/p>\n<p>if [ -z \"${d}\" ] || [ -z \"${e}\" ] || [ -z \"${a}\" ]; then<br \/>\n    usage<br \/>\nfi<br \/>\n <\/code><\/p>\n<p>Regards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>rajeshkumar created the topic: Create a script haven\u2019t been accessed for a week, then delete them Create a script for a cronjob that checks a special directory for files with&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[278],"class_list":["post-2180","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-script"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2180"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2180\/revisions"}],"predecessor-version":[{"id":2181,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2180\/revisions\/2181"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}