Last active
September 3, 2015 18:43
-
-
Save neilscudder/ce2ee9144b5fb02993b7 to your computer and use it in GitHub Desktop.
Sort TV episode files into new folders.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
main() | |
{ | |
local FILE | |
local NEWDIR | |
local WORKINGDIR="." | |
for i in "$WORKINGDIR"/*; do # Loops through all files | |
[ -d "$i" ] && continue # Skips directories | |
FILE=$(basename "$i") # Extracts filename from path | |
# Next line skips files without pattern like S01E09, S10E11 etc. | |
[[ $FILE == *"S"[0-9][0-9]* ]] || continue | |
NEWDIR=${FILE%%S[0-9]*} # Extract name preceding pattern | |
NEWDIR=${NEWDIR//./ } # Replace all dots with spaces | |
# Next line trims trailing space (http://stackoverflow.com/a/3352015/5045643) | |
NEWDIR="${NEWDIR%"${NEWDIR##*[![:space:]]}"}" | |
if [ $1 ]; then # Any argument puts script in test mode | |
echo "mv $FILE $NEWDIR" | |
else | |
mkdir "$NEWDIR" | |
mv "$FILE" "$NEWDIR" | |
fi | |
done | |
} | |
main "$@" | |
exit 0 |
Added a function to fix folders with the trailing space problem, and fixed the main function to prevent this in future. Nice substring replacement pattern I hope to one day understand, from http://stackoverflow.com/a/3352015/5045643
Works great now.
Cleanup
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BUG the new folder names have a trailing space and this causes Samba to freak out and truncate the file names dynamically. What an ass.