Skip to content

Instantly share code, notes, and snippets.

@varunchandak
Created September 30, 2025 07:38
Show Gist options
  • Save varunchandak/65de294f011308c1b10b77e9a1f1ef29 to your computer and use it in GitHub Desktop.
Save varunchandak/65de294f011308c1b10b77e9a1f1ef29 to your computer and use it in GitHub Desktop.
Organize screenshots Folder on you laptop
#!/bin/bash
# Usage: ./organize_screenshots.sh /path/to/screenshots
# Check if argument is provided
if [[ -z "$1" ]]; then
echo "❌ Error: No directory specified."
echo "Usage: $0 /path/to/screenshots"
exit 1
fi
screenshot_dir="$1"
# Check if directory exists
if [[ ! -d "$screenshot_dir" ]]; then
echo "❌ Error: Directory '$screenshot_dir' does not exist."
exit 1
fi
# Check if directory is readable
if [[ ! -r "$screenshot_dir" ]]; then
echo "❌ Error: Directory '$screenshot_dir' is not readable."
exit 1
fi
# Check if there are any files in the directory
shopt -s nullglob
filelist=("$screenshot_dir"/*)
if [ ${#filelist[@]} -eq 0 ]; then
echo "ℹ️ No files to process in $screenshot_dir"
exit 0
fi
shopt -u nullglob
# Organize files into Year-Month subfolders
for file in "${filelist[@]}"; do
# Skip directories
if [[ -d "$file" ]]; then
continue
fi
# Extract year and month from file's modification time
year_month=$(stat -f "%Sm" -t "%Y-%m" "$file")
# Create directory if it doesn't exist
target_directory="$screenshot_dir/$year_month"
if [[ ! -d "$target_directory" ]]; then
mkdir "$target_directory"
fi
# Move the file to the corresponding directory
mv "$file" "$target_directory/"
done
# Delete (move to Trash) any files older than 3 months
echo "🧹 Moving files older than 3 months to Trash..."
find "$screenshot_dir" -type f -mtime +90 -print0 | while IFS= read -r -d '' oldfile; do
echo " → Trashing: $oldfile"
trash "$oldfile"
done
echo "✅ Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment