Renaming a file using a name of another file in the same folder

Try this little snippet,

shopt -s globstar
for s in **/*.srt; do
    m=( "${s%/*}"/*.mp4 )
    printf '%s --> %s\n' "${m[0]}" "${s%.*}.mp4"
    #mv "${m[0]}" "${s%.*}.mp4"
done
shopt -u globstar
  • Remove the # in front of mv if the output is as expected
  • If there are multiple mp4s in one dir, it will rename only the first it finds. You could easily use loop to mv all mp4's and include a suffix such as _1 etc.
  • If there are multiple srts in one dir, it will rename the mp4 multiple times, so it will be named like the latest srt it finds.

Try this from the parent directory where the other sub-directories reside for a dry-run:

find -type f -name "*.srt" |
while IFS= read -r result
    do
    path="${result%/*}"
    fname="${result##*/}"
    name="${fname%.*}"
    for file in "$path"/*.{mkv,mp4,avi}
        do
        [ -e "$file" ] && echo mv -- "$file" "$path/$name.${file##*.}"
        done
    done

When you are satisfied with the output, remove echo to do the actual renaming.