Remove leading spaces from file names and folders

Hi All,

I have a vexing issue with leading spaces in file names. Basically, we're moving tons of data from our ancient afp file share to Box.com and Box forbids leading spaces in files or folders. The HFS file system seems to be perfectly fine with this, but almost all other Unix file systems are not. Of course, our users put leading spaces in some of their file names and folders so that those files and folders would show up at the top of the directory listing in the Finder. I have almost seven thousand of these instances spread out among 600 gigs of file data. My bash script already strips out illegal characters, however, I'm not sure how to add leading spaces to the script. Any advice would be greatly appreciated.

#!/bin/bash

# Find all files or folders containing 'bad' characters.
find . -depth -name "*[^a-zA-Z0-9._- ]*" |
# Read them line-by-line.
while read FILEDIR
do
       # Get the folder its inside
       DIR="${FILEDIR%/*}"
       # Get the plain name.
       FILE="${FILEDIR/*\/}"
       # Substitute _ for bad things.
       NEWFILE="${FILE//[^a-zA-Z0-9._- ]/_}"
       # Rename it.
       mv "$DIR/$FILE" "$DIR/$NEWFILE"
done
$ name="    xxxx"
$ echo "$name"
    xxxx
$ name=`echo $name`
$ echo "$name"
xxxx