{"id":130,"date":"2012-11-26T21:07:06","date_gmt":"2012-11-27T05:07:06","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=130"},"modified":"2012-12-01T17:22:22","modified_gmt":"2012-12-02T01:22:22","slug":"unix-find-remove-python-style","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/unix-find-remove-python-style\/","title":{"rendered":"Unix Find and Remove Python Style"},"content":{"rendered":"

No doubt the Unix find and remove command comes in very useful when cleaning up large folders. \u00a0However find can quickly bump into the old\u00a0\"\/usr\/bin\/find: Argument list too long\"<\/span> problem.<\/p>\n

For reference here is a command that works well. \u00a0Except of course when too many files or directories involved.<\/p>\n

find \/usr\/local\/forms\/*\/output -name "*.html" -mtime +4 -exec rm {} \\;<\/pre>\n

There is of course other ways to get this done with find, but I like python so I resorted to python as the example show below.<\/p>\n

Here is an example that worked for me:<\/p>\n

\r\n\r\n#!\/usr\/bin\/env python\r\n##  Adjust the humandays variable.  Set to 2000 days until we feel more comfortable.\r\n\r\nimport os\r\nimport glob\r\nimport time\r\nimport shutil\r\n\r\nhumandays = 2000\r\ncomputerdays = 86400*humandays\r\nnow = time.time()\r\ninputDirs = glob.glob('\/usr\/local\/forms\/*\/input')\r\n\r\nprint "Script running on %s " % time.ctime(now)\r\nprint "using physical path \/usr\/local\/forms\/*\/input and only removing directories older than %s days." % (humandays)\r\n\r\nfor inputDir in inputDirs:\r\n  for r,d,f in os.walk(inputDir):\r\n    for dir in d:\r\n         timestamp = os.path.getmtime(os.path.join(r,dir))\r\n         if now-computerdays > timestamp:\r\n             try:\r\n\t\t  print "modified: %s" % time.ctime(timestamp),\r\n\t\t  removeDir=os.path.join(r,dir)\r\n                  print "  remove ",removeDir,\r\n                  ## Better be 100% sure before you uncomment the rmtree line!\r\n                  ## shutil.rmtree(removeDir)\r\n                  ## Better be 100% sure before you uncomment the rmtree line!\r\n             except Exception,e:\r\n                  print e\r\n                  pass\r\n             else:\r\n                  print " -> success"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

No doubt the Unix find and remove command comes in very useful when cleaning up large folders. \u00a0However find can<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,13],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-bash","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}