jcanfield
6/10/2017 - 11:43 PM

Rename files in a folder to match folder name (useful for htpc usage)

Rename files in a folder to match folder name (useful for htpc usage)

#!/bin/bash
# SYPOPSIS: Rename files in folder to match folder.
# CREDIT: Unamed user on superuser or superadmin. If you find this, let me know and I will add you to the credits.
# UPCOMING: Allow for secondary input such as `rename-files-to-folder $MOVIENAME` so not to rename all movies in directory just one or two.

find * -type f -maxdepth 1 | while read file
do
    dirname="$(dirname "$file")"
    new_name="${dirname##*/}"
    file_ext=${file##*.}
    if [ -n "$file_ext" -a  -n "$dirname" -a -n "$new_name" ]
    then
        echo "mv '$file' '$dirname/$new_name.$file_ext'"
    fi
done