The best practice way to read the filenames into a bash variable is
while IFS= read -r -d '' file
Do what you want with "$file"
done < <(find $dir -type f -print0)
This works for files containing all possible characters including backspace, newline, quotes ...
The -type f
option tells find to give only names of ordinary files -- no directories, device files ...
To be fully robust the script should cope with files in the same directory with the same extension except for case, for example foo.WMV, foo.wmv ... and files with no extension and file with extensions already in lowercase.
Putting it all together (not tested):
while IFS= read -r -d '' fullpath
if [[ $filename_no_extension = $filename ]]; then
echo "$fullpath has no extension; skipping"
extension_lowercase="$( echo -n "$extension" | /usr/bin/tr '[:upper:]' '[:lower:]' )"
if [[ $extension_lowercase = $extension ]]; then
echo "$fullpath extension already lower case; skipping"
if [[ -f $newfullpath ]]; then
echo "New name $newfullpath already exists; skipping"
echo mv "$fullpath" "$newfullpath" # Remove echo after testing
done < <(find ~/Photos -type f -print0)
If you would like explanation of how any of the above works please ask.