Something like the below may give you a start, but WARNING ... if this goes wrong its likely to overwrite files. The line which actual performs the renaming is commented out below so that you can preview what actions the script will take first. So please review carefully before uncommenting that line and running for real. And taking a backup first would also be a good idea.
And as you can see below, this doesn't try to handle multiple files, it will just bail out when a directory has > 1 file.
Code:
find . -name '*.avi' -o -name '*.divx' -o -name '*.mkv' | while read path
do
filename=${path##*/}
suffix=${filename##*.}
dirpath=${path%/*}
parent=${dirpath##*/}
count=$(ls "$dirpath" | wc -l)
if [[ $count > 1 ]]; then
echo "$path -> Not renaming, more than one file in that directory"
else
echo "$path -> Renaming it to $dirpath/$parent.$suffix"
#mv "$path" "$dirpath/$parent.$suffix"
fi
done
Bookmarks