#!/bin/bash
# path ask
if [ -z "$1" ]; then
  echo "$0 <directory_path>"
  echo "giz path"
  exit 1
fi
DIR="$1"

# check valid directory 
if [ ! -d "$DIR" ]; then
  echo "'$DIR' is not valid."
  exit 1
fi

# find and replace light regex
find "$DIR" -depth -print0 | while IFS= read -r -d $'\0' file; do
  filename=$(basename "$file")
  new_filename_spaces="${filename// /_}"
  new_filename_final="${new_filename_spaces//%20/}"
  if [ "$filename" != "$new_filename_final" ]; then
    new_path=$(dirname "$file")/"$new_filename_final"
    mv -T "$file" "$new_path"
    echo "Renamed: '$file' -> '$new_path'"
  fi
done