Skip to content

Instantly share code, notes, and snippets.

@pa-0
Forked from rkitover/organize-books.sh
Created January 17, 2025 05:26
Show Gist options
  • Save pa-0/1d81f01e0b0a38281084b85939f091ec to your computer and use it in GitHub Desktop.
Save pa-0/1d81f01e0b0a38281084b85939f091ec to your computer and use it in GitHub Desktop.
organize book collection by two letter prefix
#!/bin/sh
for dir in *; do cd "$dir"; find . -maxdepth 1 -not -name . | awk '{ letters=toupper($0); gsub("[^A-Z]", "", letters); print $0 "|" dir substr(letters, index(letters, dir) + 1, 1) }' dir="$dir" | while IFS='|' read book subdir; do [ -d "$subdir" ] || mkdir "$subdir"; mv "$book" "$subdir"; done; cd ..; done
# alternately, with GNU sed instead of awk:
# for dir in *; do cd "$dir"; find . -maxdepth 1 -not -name . | gsed 'h; s/[^A-Za-z]//g; s/^\(.*\)$/\U\1/; s/^[^'$dir']*'$dir'\(.\).*/'$dir'\1/; H; g; s/\n/|/' | while IFS='|' read book subdir; do [ -d "$subdir" ] || mkdir "$subdir"; mv "$book" "$subdir"; done; cd ..; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment