Skip to content

Instantly share code, notes, and snippets.

@hobroker
Last active December 12, 2021 15:05

Revisions

  1. hobroker revised this gist Dec 12, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions group.sh
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ target="$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"

    # list files only, with the year
    # example:
    # 2020 image.jpg
    # 2021 some_filename with space.jpg
    # 2020 image.jpg
    # 2021 some_filename with space.jpg
    files="$(ls -l --time-style=+%Y $target | grep '^-' | tr -s ' ' | cut -d " " -f 6- | awk NF)"

    while IFS= read -r line; do
  2. hobroker created this gist Dec 12, 2021.
    30 changes: 30 additions & 0 deletions group.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/usr/bin/env bash
    #
    # Usage: group.sh target_directory
    # Result:
    # target_directory/2020/...files from 2020
    # target_directory/2021/...files from 2021
    #

    set -e

    target="$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"

    # list files only, with the year
    # example:
    # 2020 image.jpg
    # 2021 some_filename with space.jpg
    files="$(ls -l --time-style=+%Y $target | grep '^-' | tr -s ' ' | cut -d " " -f 6- | awk NF)"

    while IFS= read -r line; do
    IFS=" " read year filename <<< "$line"
    dir="$target/$year"
    file="$target/$filename"

    printf "%s <- %s" $dir $file

    mkdir -p "$dir"
    mv "$file" "$dir"

    printf " $\n"
    done <<< "$files"