there was a post previously about this from around 2010
but i was unable to get the suggested scripts there to work.
the following code works for me when it's saved inside the
directory of files whose names i want to shorten, but i would
like to be able to store it in a file with a list of
directories and have it enter into each directory, shorten the
file names in that directory, and then move to the next
directory.
basically enter directory1 and perform the code below,
move to directory2 and do it again, then directory3, etc. etc.
thanks in advance for any help!
rename_seq ()
{
seq=1
oldname="$1"
newname="$2"
ext="$3"
while [ -e "$newname$(printf "%02d" $seq).$ext" ]; do
(( seq ++ ))
done
mv "$oldname.$ext" "$newname$(printf "%02d" $seq).$ext"
}
ls | while read name; do
ext="${name##*.}"
name="${name%.*}"
newname="${name//[<>=?:;\"*+,|]/_}"
newname="${newname:0:38}"
if [ -e "${newname}.$ext" ]; then
rename_seq "$newname" "$newname" "$ext"
fi
if [ -e "${newname}"??".$ext" ]; then
rename_seq "$name" "$newname" "$ext"
else mv "$name.$ext" "$newname.$ext"
fi
done