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.walkand filter out files based on ctime/mtime, which you can get like this:If you prefer to do it with shell, then
findis your friend, with the following flags:[edit]
A small code example to get modification time of files in a given dir (“.”):
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.