2023 12 17


Tags: unix
Reading time: about 1 minute

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}")

Citation

If you find this work useful, please cite it as:
@article{yaltirakli,
  title   = "2023 12 17",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/journal/2023-12-17"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "2023 12 17", November, 2024. [Online]. Available: https://www.gkbrk.com/journal/2023-12-17. [Accessed Nov. 12, 2024].
APA Style
Yaltirakli, G. (2024, November 12). 2023 12 17. https://www.gkbrk.com/journal/2023-12-17
Bluebook Style
Gokberk Yaltirakli, 2023 12 17, GKBRK.COM (Nov. 12, 2024), https://www.gkbrk.com/journal/2023-12-17

Comments

© 2024 Gokberk Yaltirakli