Skip to content

Instantly share code, notes, and snippets.

@boyvanamstel
Last active November 23, 2018 11:21
Show Gist options
  • Save boyvanamstel/50a4365f51325d56fcd597c414ab0e44 to your computer and use it in GitHub Desktop.
Save boyvanamstel/50a4365f51325d56fcd597c414ab0e44 to your computer and use it in GitHub Desktop.
Game organizer bash script.
#!/bin/sh
#
# Organizes a directory of games into a more convenient layout:
#
# Games/
# - 0-9/
# - A/
# - B-D/
# - E-H/
# - I/
# etc. etc.
#
# Copy this script into /usr/local/bin.
#
# Usage: organize_games max
#
# $max is the maximum amount of games per directory.
# This is merely a suggestion. If there are 100 games that start with S, setting
# $max to 25 still creates a directory of 100 games.
#
# Source: https://gist.github.com/boyvanamstel/50a4365f51325d56fcd597c414ab0e44
if [ -z "$1" ]; then
echo "Usage: organize_games max"
exit 1
fi
max=$1
sum=0
chars=()
dirs=("0-9")
for x in {a..z}; do
X=`echo $x | awk '{print toupper($0)}'`
chars+=("$X")
count=`ls ["$x$X"]* | wc -l | bc`
sum=`expr $sum + $count`
if [ "$sum" -gt "$max" ] || [ "$X" == "Z" ]; then
path="${chars[0]}"
if [ "${#chars[@]}" -gt 1 ]; then
path="${chars[0]}-${chars[${#chars[@]}-1]}"
fi
dirs+=("$path")
sum=0
chars=()
continue
fi
done
for path in "${dirs[@]}"; do
if [ -n "$path" ]; then
echo "Processing $path..."
mkdir "$path"
find . -maxdepth 1 -type f -iregex "\./[$path].*" -exec cp "{}" "$path" \;
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment