How to replace spaces in file names using a bash script

This will recursivly replace spaces with '_' in directories and filenames

find . -depth -name '* *' \
| while IFS= read -r f ; do mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr ' ' _)" ; done