Image sorting helper script

As you know, I do all my file management with a CLI interface, Midnight Commander and scripts. It’s a slight challenge at first to manage pictures and videos, but it turns out to be much more efficient than a GUI as tools are learned and honed. It’s also more 1337 and future proof. Yeah, 1337.

#!/bin/bash
#
# sort.sh
#
# Cycle through all image files in a directory and prompt for a
# directory in which to move them. Useful for organizing downloaded pics
# after a 4chan, ffffound or other imageboard hunt.
#
# Copyright (c) 2011 Alexandre de Verteuil
# No restrictions. None.

VIEWER="feh --scale-down"
GIFVIEWER="gifview --animate"

f_view () {
    # Spawn the appropriate viewer.
    # Return 1 if not a supported file extension.
    ext=${candidate##.*\.}  # Get extension.
    ext=${ext,,}  # Lowercase.
    case $ext in
        jpg|jpeg|png) $VIEWER "$candidate";;
        gif) $GIFVIEWER "$candidate";;
        *) echo Skipping "${candidate}"...
           return 1
           ;;
    esac
    }

for candidate in $(find . -maxdepth 1 -type f); do
    echo -----------------------------------------------------
    # Show an image
    f_view "$candidate" || continue
    # Print available directories and prompt for a target.
    # Rebuild list of dirs because dirs may be created in the process.
    unset -v dirs
    dirs[0]="."
    i=1
    while read dir; do
        echo $i: $dir
        dirs[$((i++))]="$dir"
    done < <(find . -mindepth 1 -type d -printf '%p\n' | sort)
    echo "Move $candidate to..."
    read -p "...target dir (New,Quit): " target
    case $target in
        ""|"0") continue;;
        n|N) read -p "New directory name: " dirname
             mkdir -v "$dirname" && mv -v "$candidate" "$dirname"
             ;;
        q|Q|quit|exit) exit;;
        *) if [ -d "${dirs[$target]}" ]; then
               mv -v "$candidate" "${dirs[$target]}"
           fi
           ;;
    esac
    sleep 0.5s
done

Possible feature expansion include:

  • subdirectory recursion
  • multiple column output
  • exploiting readline facility
  • file renaming

I also plan using a file naming structure such as <id>-tag_tag_tag.<ext> where id is a few random alphanumeric digits. This way, I can use a locate database specifically for my pictures and multimedia or I can just grep directories.

Alexandre de Verteuil
Alexandre de Verteuil
Senior Solutions Architect

I teach people how to see the matrix metrics.
Monkeys and sunsets make me happy.

Related