-
-
Save Xe/947d90506da90cd16d3ca91a9f892f15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # gather-jpegs.sh | |
| # Usage: ./gather-jpegs.sh /path/to/source /path/to/destination | |
| set -euo pipefail | |
| if [ "$#" -ne 2 ]; then | |
| echo "Usage: $(basename "$0") SOURCE_DIR DEST_DIR" >&2 | |
| exit 1 | |
| fi | |
| src=$1 | |
| dst=$2 | |
| if [ ! -d "$src" ]; then | |
| echo "Source directory does not exist: $src" >&2 | |
| exit 1 | |
| fi | |
| mkdir -p "$dst" | |
| export LC_ALL=C | |
| # Find all JPEGs (case-insensitive), then copy with organized structure. | |
| find "$src" -type f \( -iname '*.jpg' -o -iname '*.jpeg' \) -print0 | | |
| while IFS= read -r -d '' f; do | |
| base=$(basename "$f") | |
| name=${base%.*} | |
| ext=${base##*.} | |
| # Parse filename format: timestamp-titleID.ext | |
| if [[ "$name" =~ ^([0-9]+)-([0-9A-F]+)$ ]]; then | |
| timestamp="${BASH_REMATCH[1]}" | |
| title_id="${BASH_REMATCH[2]}" | |
| # Create subfolder for this title ID | |
| title_dir="$dst/$title_id" | |
| mkdir -p "$title_dir" | |
| # Target filename is just the timestamp | |
| target="$title_dir/$timestamp.$ext" | |
| # If a file with the same timestamp already exists, add a number suffix | |
| if [ -e "$target" ]; then | |
| i=1 | |
| target="$title_dir/${timestamp}_${i}.$ext" | |
| while [ -e "$target" ]; do | |
| i=$((i + 1)) | |
| target="$title_dir/${timestamp}_${i}.$ext" | |
| done | |
| fi | |
| else | |
| # Fallback for files that don't match the expected format | |
| echo "Warning: File doesn't match expected format (timestamp-titleID): $base" | |
| target="$dst/$base" | |
| # If a file with the same name already exists, make a unique target name. | |
| if [ -e "$target" ]; then | |
| i=1 | |
| target="$dst/${name}_${i}.${ext}" | |
| while [ -e "$target" ]; do | |
| i=$((i + 1)) | |
| target="$dst/${name}_${i}.${ext}" | |
| done | |
| fi | |
| fi | |
| # -p preserves timestamps and mode; add -n to avoid overwrite, but we already ensure uniqueness. | |
| cp -p "$f" "$target" | |
| echo "Copied: $target" | |
| done | |
| echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment