Skip to content

Instantly share code, notes, and snippets.

@neilscudder
Last active September 3, 2015 18:43
Show Gist options
  • Save neilscudder/ce2ee9144b5fb02993b7 to your computer and use it in GitHub Desktop.
Save neilscudder/ce2ee9144b5fb02993b7 to your computer and use it in GitHub Desktop.
Sort TV episode files into new folders.
#!/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
@neilscudder
Copy link
Author

Cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment