完全過度設計相比,使用find + xargs的一個bash一個襯墊,但這裏是一個快速的Python腳本,一部分來自我之前寫了一些其他的腳本混搭。應該適合你的目的。
現在我去努力的唯一原因是因爲評論你做的:
是的,我可以花一個月+學習這些語言
它非常值得的努力之一,並且很快就會付諸東流。 這個腳本花了大約7分鐘時間做了一些測試。
#!/usr/bin/python
import datetime
import os
import sys
import shutil
SOURCE_PATH = "/home/customers/"
TARGET_PATH = "/home/oldcustomers/"
TIME_THRESHOLD = datetime.timedelta(365) #days
def get_old_dirs(source_path, time_threshold):
old_dirs = []
for root, dirs, files in os.walk(source_path):
for d in dirs:
full_path = os.path.join(root, d)
now = datetime.datetime.now()
last_modified = datetime.datetime.fromtimestamp(os.stat(full_path).st_mtime)
delta = now - last_modified
if (delta) >= time_threshold:
old_dirs.append((full_path, delta))
break
return old_dirs
def move_old_dirs(target_path, source_path, time_threshold, confirm=True):
dirs = get_old_dirs(source_path, time_threshold)
print '"old" dirs: %d' % len(dirs)
if dirs:
if confirm:
print "pending moves:"
for (d, delta) in dirs:
print "[%s days] %s" % (str(delta.days).rjust(4), d)
if not raw_input("Move %d directories to %s ? [y/n]: " % (len(dirs), target_path)).lower() in ['y', 'yes']:
return
if not os.path.exists(target_path):
os.makedirs(target_path)
for (d, delta) in dirs:
shutil.move(d, target_path)
print "%s -> %s" % (d, target_path)
print "moved %d directories" % len(dirs)
def cmdline(args):
from optparse import OptionParser
usage = "move_old_dirs [options] <source_dir> <target_dir>"
default_desc = "%s -> %s [%s]" % (SOURCE_PATH, TARGET_PATH, TIME_THRESHOLD)
parser = OptionParser(usage)
parser.add_option("-d", "--days",
action="store", type="int", dest="days", default=365,
help="How many days old the directory must be to move")
parser.add_option("--default", default=False,
action="store_true", dest="default",
help="Run the default values set in the script: (%s)" % default_desc)
parser.add_option("-f", "--force", default=False,
action="store_true", dest="force",
help="Dont ask for confirmation")
(options, args) = parser.parse_args(args)
if len(args) == 1 and options.default:
print "running default: %s" % default_desc
return move_old_dirs(TARGET_PATH, SOURCE_PATH, TIME_THRESHOLD, confirm=(not options.force))
elif len(args) == 3:
return move_old_dirs(args[2], args[1], datetime.timedelta(options.days), confirm=(not options.force))
print usage
print "incorrect number of arguments, try -h or --help"
return 1
if __name__ == "__main__":
cmdline(sys.argv)
剛剛扔掉的是,在PATH某些文件(如move_old_dirs
),chmod命令可執行文件和一展身手。
我認爲這項工作可以用這三種語言中的任何一種很容易地完成。 – hochl 2012-03-10 16:15:14