find-mtime again
In November 2020, I wrote an article called “How I keep track of what I’ve been working on” where I described using the find
command with the -mtime
option to find files and projects that I’ve worked on recently.
I’ve been using this technique for a while now, and it has expanded into a shell script with lots of grep filters, and stat commands to print the modification time of the files. This became brittle with weird file paths, and quite slow to run.
I thought it was finally time to remake this script, and cross off the “Rewrite the mtime.sh script in Python to have improved filtering and performance” item from my todo list.
The new script
Here’s what I ended up with:
#!/usr/bin/env python3
import tqdm
import datetime
from pathlib import Path
DIRS = [
# ...
]
EXCLUDES = [
"/__pycache__",
"/.git/",
"/.idea/",
# ...
]
files = []
file_to_mtime = {}
for d in DIRS:
for p in Path(d).rglob("*"):
if any([e in str(p) for e in EXCLUDES]): continue
if p.is_dir(): continue
if not p.is_file: continue
if p.is_symlink(): continue
files.append(p)
for f in tqdm.tqdm(files):
file_to_mtime[str(f)] = f.stat().st_mtime
files.sort(key=lambda x: file_to_mtime[str(x)])
for f in files:
mt = file_to_mtime[str(f)]
dt = datetime.datetime.fromtimestamp(mt)
print(f"{dt:%Y-%m-%d %H:%M:%S} {f}")