Created
August 30, 2026 22:37
-
-
Save davehull/12419d044b5d88712689c3e203063e1d to your computer and use it in GitHub Desktop.
Useful for renaming media files and their associated sidecar files
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
| #!/bin/bash | |
| # Default flag values | |
| VERBOSE=false | |
| WHAT_IF=false | |
| # Parse flags and arguments | |
| ARGS=() | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| --verbose|-v) | |
| VERBOSE=true | |
| shift | |
| ;; | |
| --whatif) | |
| WHAT_IF=true | |
| shift | |
| ;; | |
| -*) | |
| echo "Error: Unknown option $1" >&2 | |
| exit 1 | |
| ;; | |
| *) | |
| ARGS+=("$1") | |
| shift | |
| ;; | |
| esac | |
| done | |
| # Ensure we have exactly two main arguments (source and destination) | |
| if [ ${#ARGS[@]} -ne 2 ]; then | |
| echo "Usage: $(basename "$0") [--verbose] [--whatif] <source_file> <new_name.ext>" | |
| exit 1 | |
| fi | |
| SRC_FILE="${ARGS[0]}" | |
| DEST_FILE="${ARGS[1]}" | |
| # Verify source file actually exists | |
| if [ ! -f "$SRC_FILE" ]; then | |
| echo "Error: Source file '$SRC_FILE' not found." >&2 | |
| exit 1 | |
| fi | |
| # Extract components | |
| SRC_BASE="${SRC_FILE%.*}" | |
| DEST_BASE="${DEST_FILE%.*}" | |
| # 1. Handle the primary media file | |
| if [ "$WHAT_IF" = true ]; then | |
| echo "[What If] Would rename: '$SRC_FILE' -> '$DEST_FILE'" | |
| else | |
| if [ "$VERBOSE" = true ]; then | |
| echo "Renaming: '$SRC_FILE' -> '$DEST_FILE'" | |
| fi | |
| mv -n "$SRC_FILE" "$DEST_FILE" | |
| fi | |
| # 2. Handle standard sidecar files (Exact basename match: .LRF, .SRT, .XMP, .AAE) | |
| SIDECAR_EXTS=("lrf" "srt" "xmp" "aae") | |
| for ext in "${SIDECAR_EXTS[@]}"; do | |
| ext_upper=$(echo "$ext" | tr '[a-z]' '[A-Z]') | |
| for s_ext in "${ext}" "${ext_upper}"; do | |
| SIDECAR_SRC="${SRC_BASE}.${s_ext}" | |
| if [ -f "$SIDECAR_SRC" ]; then | |
| SIDECAR_DEST="${DEST_BASE}.${ext_upper}" | |
| if [ "$WHAT_IF" = true ]; then | |
| echo "[What If] Would rename sidecar: '$SIDECAR_SRC' -> '$SIDECAR_DEST'" | |
| else | |
| if [ "$VERBOSE" = true ]; then | |
| echo "Renaming sidecar: '$SIDECAR_SRC' -> '$SIDECAR_DEST'" | |
| fi | |
| mv -n "$SIDECAR_SRC" "$SIDECAR_DEST" | |
| fi | |
| break | |
| fi | |
| done | |
| done | |
| # 3. Handle Sony Metadata / Multi-part XML files (e.g., C0193M01.XML or C0193M01.xml) | |
| for xml_file in "${SRC_BASE}"M*.[xX][mM][lL]; do | |
| # Check if the glob found a real file | |
| if [ -f "$xml_file" ]; then | |
| # Extract the suffix portion attached to the basename (e.g., "M01") | |
| suffix="${xml_file#$SRC_BASE}" # Leaves "M01.XML" or "M01.xml" | |
| suffix="${suffix%.*}" # Leaves "M01" | |
| # Build destination with preserved 'M' suffix and forced UPPERCASE .XML | |
| XML_DEST="${DEST_BASE}${suffix}.XML" | |
| if [ "$WHAT_IF" = true ]; then | |
| echo "[What If] Would rename Sony metadata: '$xml_file' -> '$XML_DEST'" | |
| else | |
| if [ "$VERBOSE" = true ]; then | |
| echo "Renaming Sony metadata: '$xml_file' -> '$XML_DEST'" | |
| fi | |
| mv -n "$xml_file" "$XML_DEST" | |
| fi | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment