简体   繁体   中英

getting files in a directory modified within a date range in linux using python

I am trying to write code that will fetch the files in a directory that have been created/modified within a specific date range.

I do not know much about linux and I would like to know what command I can use to get a list of files in a directory that match within a date range I specify.

also, what is the correct formating for this type of query, as this process will be automated and the user needs to just put in his start and end dates.

the relevant code so far:

#! /usr/bin/env python

import os
import copy
import subprocess
import optparse

def command(command):
    env = copy.deepcopy(os.environ)
    proc = subprocess.Popen([command],
                shell=True, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result = proc.stdout.read()

if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option("-s", "--startdate", dest = "startdate",\
                      help = "the starting date of the files to search")
    parser.add_option("-e", "--enddate", dest = "enddate",\
                      help = "the ending date of the files to search")
    (options, args) = parser.parse_args()

    # commands
    file_names = command("get files that match dates command")

What must I put in that command to get these file names?

EDIT:

conversely - it does not have to be a command, if it can be done using pure code, such as os.walk for instance, that is also great. I know that certain features dont work exactly in Linux and Windows, so help on this matter would be warranted.

EDIT 2:

Regardless of the method, the user should input two dates: start and end. and then get all files that are modified/created between those dates.

One option would be to use something in lines of os.walk and filter out files based on ctime/mtime, which you can get like this:

import os.path, time
print "last modified: %s" % time.ctime(os.path.getmtime(file))
print "created: %s" % time.ctime(os.path.getctime(file))

If you prefer to do it with shell, then find is your friend, with the following flags:

-ctime n File's status was last changed n*24 hours ago.

-mtime n File's data was last modified n*24 hours ago.

[edit]

A small code example to get modification time of files in a given dir ("."):

import os
from os.path import join
import datetime


def modification_date(filename):
        t = os.path.getmtime(filename)
        return t

def creation_date(filename):
        t = os.path.getctime(filename)
        return t

for root, dirs, files in os.walk("."):
        for name in files:
        print join(root, name), modification_date(join(root, name)), creation_date(join(root, name))

Depending on your particular commandline parameters implementation you want to convert what's passed on commandline to a unix timestamp and compare with either of the dates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM