Skip to content

Instantly share code, notes, and snippets.

@Marcelektro
Created May 29, 2025 11:21
Show Gist options
  • Save Marcelektro/7147898aa5b69ec5f180329d5354cee2 to your computer and use it in GitHub Desktop.
Save Marcelektro/7147898aa5b69ec5f180329d5354cee2 to your computer and use it in GitHub Desktop.
Screenshots directory sorter (./<year>/<month>/<day>/<filename>)

Screenshot Organizer

Having set up my (Gnome) system years ago, I took screenshots almost daily.
The ~/Pictures/Screenshots directory had over 9k items. Browsing thru them was pain.

This simple script lets you quickly organize all loose files in given directory (-d flag) into a ./<year>/<month>/<day>/<filename> structure.

#!/usr/bin/env python3
from pathlib import Path
from datetime import datetime
import shutil
import argparse
def organize_screenshots(base_dir):
base_path = Path(base_dir).expanduser().resolve()
if not base_path.is_dir():
print(f"Error: `{base_path}` is not a directory.")
return
total_moved_files = 0
total_failed_files = 0
for item in base_path.iterdir():
if not item.is_file():
continue
try:
mtime = item.stat().st_mtime
date = datetime.fromtimestamp(mtime)
year = f"{date.year:04d}"
month = f"{date.month:02d}"
day = f"{date.day:02d}"
target_dir = base_path / year / month / day
target_dir.mkdir(parents=True, exist_ok=True)
destination = target_dir / item.name
counter = 1
while destination.exists():
stem = item.stem
suffix = item.suffix
destination = target_dir / f"{stem}_{counter}{suffix}"
print(f"File `{destination.name}` already exists. Attempting rename to `{destination.name}`...")
counter += 1
shutil.move(str(item), str(destination))
print(f"Moved: `{item.name}` -> `{destination.relative_to(base_path)}`")
total_moved_files += 1
except Exception as e:
print(f"Error processing `{item.name}`: {e}")
total_failed_files += 1
print(f"\nTotal files moved: {total_moved_files}")
print(f"Total files failed to move: {total_failed_files}")
def main():
parser = argparse.ArgumentParser(description="Organize screenshots by date modified.")
parser.add_argument("-d", "--directory", required=True, help="Directory containing the screenshots")
args = parser.parse_args()
organize_screenshots(args.directory)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment