{"id":24,"date":"2016-12-20T06:33:12","date_gmt":"2016-12-20T06:33:12","guid":{"rendered":"http:\/\/conciseusa.com\/wordpress\/?p=24"},"modified":"2021-08-29T19:29:39","modified_gmt":"2021-08-29T19:29:39","slug":"script-to-limit-number-of-files-in-a-directory","status":"publish","type":"post","link":"https:\/\/conciseusa.com\/wordpress\/script-to-limit-number-of-files-in-a-directory\/","title":{"rendered":"Script to Limit Number of Files in a Directory"},"content":{"rendered":"<p>I have some jobs that run and will fill up disk drives if not pruned.<\/p>\n<p>I put together the simple script below that I can call with a directory and the number of files I want to keep. The oldest files will be deleted. I add it to cron jobs with a command in this pattern:<\/p>\n<pre>~\/DelOverNumberFiles.sh ~\/public_html\/FrontDoor\/Camera\\ 01 200\r\nNote: there are 2 params, the \\ escapes the space before the 01<\/pre>\n<pre>Copy into DelOverNumberFiles.sh\r\n\r\n#!\/bin\/bash\r\n\r\nlimit=$2\r\ndir=$1\r\nCnt=0\r\nfor line in `ls -t \"$1\"`\r\ndo\r\n  if [[ $Cnt -gt $limit ]]\r\n  then\r\n    rm \"$dir\/$line\"\r\n    echo \"rm $dir\/$line\"\r\n  fi\r\n  Cnt=`expr $Cnt + 1`\r\ndone<\/pre>\n<p>While I know this is not the most professional way to do this, I wanted something very simple I could play with and should have few dependencies so I could use it on shared servers that I do not have control over.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have some jobs that run and will fill up disk drives if not pruned. I put together the simple script below that I can call with a directory and the number of files I want to keep. The oldest &hellip; <a href=\"https:\/\/conciseusa.com\/wordpress\/script-to-limit-number-of-files-in-a-directory\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":1,"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":25,"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/posts\/24\/revisions\/25"}],"wp:attachment":[{"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/conciseusa.com\/wordpress\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}