Skip to content

Instantly share code, notes, and snippets.

@theKlanc
Last active April 10, 2017 14:10
Show Gist options
  • Save theKlanc/bc13d068f583acf664e3e8ac7d6b96d2 to your computer and use it in GitHub Desktop.
Save theKlanc/bc13d068f583acf664e3e8ac7d6b96d2 to your computer and use it in GitHub Desktop.
Renamer script (mainly for anime/tv series)
#!/bin/bash
function preguntar {
while IFS='\n' read -r line || [[ -n "$line" ]]; do
extension="${line##*.}"
filename=$(basename "$line")
filename="${filename%.*}"
IFS=$4
number="$(echo $filename | sed 's/ \+/ /g' | cut -d" " -f$1)"
newname=$number$5$filename"."$extension
if [ "$2" = true ] ; then
newname=$number$5$3"."$extension
fi
line="\"$filename.$extension\""
newname="\"$newname\""
echo "$line -> $newname"
done
}
function renombrar {
while IFS='' read -r line || [[ -n "$line" ]]; do
extension="${line##*.}"
filename=$(basename "$line")
filename="${filename%.*}"
IFS=$4
number="$(echo $filename | sed 's/ \+/ /g' | cut -d" " -f$1)"
newname=$number$5$filename"."$extension
if [ "$2" = true ] ; then
newname=$number$5$3"."$extension
fi
line="\"$filename.$extension\""
newname="\"$newname\""
eval "mv $line $newname"
done
}
nom="Gintama"
posicio=3
netejarNom=false
originSeparator="\ "
resultSeparator="\ "
if [[ $# = "0" ]]
then
echo "Usage: renamer [OPTIONS] NAME NUMBERPOSITION..."
echo "Try 'renamer -h' for more information."
exit
fi
while (( "$#" ))
do
toshift=
case $1 in
"-n")
netejarNom=true
toshift=1
;;
"-o")
originSeparator=$2
toshift=2
;;
"-r")
resultSeparator=$2
toshift=2
;;
"-h")
echo "Usage: renamer [OPTIONS] NAME NUMBERPOSITION..."
echo "Rename all files containing NAME by moving the NUMBERPOSITIONth word to the start"
echo " -h show this help dialog"
echo " -n create a new name with the format \"NUMBERPOSITIONth NAME.extension\""
echo " -o define the separator used for the source files"
echo " -r define the separator used for the results"
toshift=1
exit
;;
*)
nom=$1
posicio=$2
toshift=2
;;
esac;
shift $toshift
done
ls -ld * .* | sed 's/ \+/ /g' | cut -d" " -f9- | grep . | grep $nom | preguntar $posicio $netejarNom $nom $originSeparator $resultSeparator
echo "ARE YOU SURE ABOUT THAT? (Y/n)"
read confirmacio
if [[ $confirmacio == *"Y"* ]]; then
ls -ld * .* | sed 's/ \+/ /g' | cut -d" " -f9- | grep . | grep $nom | renombrar $posicio $netejarNom $nom $originSeparator $resultSeparator
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment