move all folders modified after <time> to new folder

275 views Asked by At

I need to write a script that moves all folders within a given parent-directory that are modified after a given time. I want to either use bash or Python.

so it should be something like.

forall ${DIR} in ${PARENT_DIR}
If ${DIR} is modified after ${TIME}
move ${DIR} to ${NEW_DIR}

It has to check the modification of the directories every 15 min and move any newly created directories.

Thanks for the help

1

There are 1 answers

0
John Wilson On BEST ANSWER
import os
from shutil import move
from time import time

def mins_since_mod(fname):
    """Return time from last modification in minutes"""
    return (time() - os.path.getmtime(fname)) / 60

PARENT_DIR = '/some/directory'
MOVE_DIR = '/where/to/move'

# Loop over files in PARENT_DIR
for fname in os.listdir(PARENT_DIR):
    # If the file is a directory and was modified in last 15 minutes
    if os.path.isdir(fname) and mins_since_mod(fname) < 15:
        move(fname, MOVE_DIR) # move it to a new location