Skip to content

Instantly share code, notes, and snippets.

@jivanpal
Last active February 13, 2024 19:10
Show Gist options
  • Save jivanpal/116032ab6329fa5eaef88c9e9f46f2a3 to your computer and use it in GitHub Desktop.
Save jivanpal/116032ab6329fa5eaef88c9e9f46f2a3 to your computer and use it in GitHub Desktop.

Revisions

  1. jivanpal revised this gist Feb 13, 2024. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions google-photos-reorganise-2021.sh
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,11 @@ cd undated

    for j in *.json; do
    unix_timestamp="$(jq -r .photoTakenTime.timestamp "$j")"
    if [ $? -ne 0 ] ; then
    >&2 echo "Timestamp not found in $j"
    continue
    fi

    date="$(date -d "@$unix_timestamp" +%Y/%m/%d)"
    basename="$(echo "$j" | grep -Eo '^[^\.]*')"
    destination="../dated/$date"
  2. jivanpal revised this gist Feb 13, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google-photos-reorganise-2021.sh
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ cd undated
    for j in *.json; do
    unix_timestamp="$(jq -r .photoTakenTime.timestamp "$j")"
    date="$(date -d "@$unix_timestamp" +%Y/%m/%d)"
    basename="$(echo "$j" | grep -Eo -- '^[^\.]*')"
    basename="$(echo "$j" | grep -Eo '^[^\.]*')"
    destination="../dated/$date"

    mkdir -p -- "$destination"
  3. jivanpal revised this gist Jul 15, 2023. 1 changed file with 5 additions and 13 deletions.
    18 changes: 5 additions & 13 deletions google-photos-reorganise-2021.sh
    Original file line number Diff line number Diff line change
    @@ -18,19 +18,11 @@ IFS=$'\n'
    cd undated

    for j in *.json; do
    unix_timestamp="$( \
    (grep -EA1 photoTakenTime "$j" || >&2 echo "No photoTakenTime in $j") \
    | (grep -E timestamp || >&2 echo "No timestamp in $j") \
    | sed -E 's/ +/ /g' | sed -E 's/(^ | $)//g' \
    | cut -d' ' -f2 \
    | (grep -E '^\"[0-9]*\",$' || >&2 echo "Timestamp not in expected format in $j") \
    | sed -E 's/^\"([0-9]*)\",$/\1/' \
    )"

    unix_timestamp="$(jq -r .photoTakenTime.timestamp "$j")"
    date="$(date -d "@$unix_timestamp" +%Y/%m/%d)"
    basename="$(echo "$j" | grep -Eo '^[^\.]*')"
    basename="$(echo "$j" | grep -Eo -- '^[^\.]*')"
    destination="../dated/$date"

    mkdir -p "$destination"
    mv "$basename".* "$destination/"
    done
    mkdir -p -- "$destination"
    mv -- "$basename"* "$destination/"
    done
  4. jivanpal created this gist Jun 9, 2021.
    26 changes: 26 additions & 0 deletions google-photos-reorganise-2020.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/bin/bash

    ## Works with the Google Takeout format from 2020 and earlier, in which
    ## items are already within directories of the form "YYYY-MM-DD", and you
    ## want a directory hierarchy of the form "YYYY/MM/DD".
    ##
    ## You must run this script from the directory which contains all the
    ## directories of the form "YYYY-MM-DD". The resulting directories of the
    ## form "YYYY/MM/DD" will be created in this same location.

    trap '>&2 echo Interrupted. && exit 1' SIGINT

    if [ "$1" = "-n" ] || [ "$1" = "--dry-run" ]; then
    maybe_echo='echo'
    else
    maybe_echo=''
    fi

    for dir in ????-??-??*/ ; do
    year="$( echo "$dir" | grep -Po '^[0-9]{4}' )"
    month="$( echo "$dir" | grep -Po '(?<=^[0-9]{4}-)[0-9]{2}' )"
    rest="$( echo "$dir" | grep -Po '(?<=^[0-9]{4}-[0-9]{2}-).*(?=/$)' )"

    $maybe_echo mkdir -p "$year/$month"
    $maybe_echo mv "$dir" "$year/$month/$rest"
    done
    36 changes: 36 additions & 0 deletions google-photos-reorganise-2021.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/bash

    ## Works with the Google Takeout format from 2021 and later, in which
    ## items are all in a single directory (no timestamp necessarily in file
    ## or directory names), but JSON files contain Unix timestamps, and you
    ## want a directory hierarchy of the form "YYYY/MM/DD".
    ##
    ## You must start with all items in a folder called `undated`, and run this
    ## script from its parent. A folder called `dated` will then be created with
    ## the "YYYY/MM/DD" hierarchy within it. This will leave all files which we
    ## cannot sort in the `undated` directory for you to sort yourself.

    trap '>&2 echo Interrupted. && exit 1' SIGINT

    OLDIFS=$IFS
    IFS=$'\n'

    cd undated

    for j in *.json; do
    unix_timestamp="$( \
    (grep -EA1 photoTakenTime "$j" || >&2 echo "No photoTakenTime in $j") \
    | (grep -E timestamp || >&2 echo "No timestamp in $j") \
    | sed -E 's/ +/ /g' | sed -E 's/(^ | $)//g' \
    | cut -d' ' -f2 \
    | (grep -E '^\"[0-9]*\",$' || >&2 echo "Timestamp not in expected format in $j") \
    | sed -E 's/^\"([0-9]*)\",$/\1/' \
    )"

    date="$(date -d "@$unix_timestamp" +%Y/%m/%d)"
    basename="$(echo "$j" | grep -Eo '^[^\.]*')"
    destination="../dated/$date"

    mkdir -p "$destination"
    mv "$basename".* "$destination/"
    done