Skip to content

Instantly share code, notes, and snippets.

@noelyahan
Last active December 7, 2015 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noelyahan/e1ba7460f67629089d9c to your computer and use it in GitHub Desktop.
Save noelyahan/e1ba7460f67629089d9c to your computer and use it in GitHub Desktop.
# run instructions : python find.py / -m "2015-12-07 21:25:29" 30
import sys
import os
from datetime import datetime, timedelta
TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S";
# file mod info infomation hashmap
rwx_hash = {1:"--x", 2:"-w-", 4:"r--", 3:"-wx", 5:"r-x", 6:"rw-", 7:"rwx"}
# path, timestamp_type, input_typestamp, mins range
# input parameters to an array
args = sys.argv
# starting file path to search
search_path = args[1]
# type timestamp type Access -a | Modified -m | Changed -c
timestamp_type = args[2]
# timestamp of the file
input_file_timestamp = args[3].split(".")[0]
# minutes range to search files
mins_range = args[4]
# convert string date to datetime object
input_file_timestamp = datetime.strptime(input_file_timestamp, TIMESTAMP_FORMAT)
# new timestamp of the file after adding the input minutes
new_file_timestamp = input_file_timestamp + timedelta(minutes=int(mins_range))
# search for the files
for dir_path, sub_dir_list, file_list in os.walk(search_path):
for fname in file_list:
# hidden files filter
if(fname.startswith(".")):
try:
# get absolute file path
abs_file_path = os.path.abspath(os.path.join(dir_path, fname));
# get all the stat of the current file
stats = os.stat(abs_file_path)
# filter timestamp according to input type (access, modified, changed)
if(timestamp_type == "-a"):
current_file_timestamp = stats.st_atime
elif(timestamp_type == "-m"):
current_file_timestamp = stats.st_mtime
else:
current_file_timestamp = stats.st_ctime
# get proper timestam format for datetime comparison
current_file_timestamp = datetime.fromtimestamp(int(current_file_timestamp)).strftime(TIMESTAMP_FORMAT)
# convert string time datetime object
current_file_timestamp = datetime.strptime(current_file_timestamp, TIMESTAMP_FORMAT)
# compare and output the file info
if (current_file_timestamp > input_file_timestamp and current_file_timestamp < new_file_timestamp):
ugo_rwx_str = "";
for i in map(int, oct(stats.st_mode)[4:]):
ugo_rwx_str += rwx_hash[i]
# output the result with file mod info
print "-"+ugo_rwx_str+" "+dir_path+"/"+fname
except:
continue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment