tr uses [:blank:] for horizontal spaces ( man tr ) and I just happen to have a diddy for such a rename project.
Make it executable and run like this: ./test /home/myimages
Note: the mv statement is protected by " echo " so you can test it with no harm done. Just remove the echo when ready to roll.
Note: this site changed my [[ : alnum : ]] , I haven't tested it with just one [ ]
Also, you can use sed to make the replacement:
j=`echo -n "$i" | sed -e 's/ /_/'`
Code:
#!/bin/sh
#
usage()
{
echo "Usage: $0 [directory]"
exit 1;
}
test -d "$1" || usage
dir="$1"
ls $dir | grep -e "[:alnum:]" | \
while read i; do
j=`echo -n "$i" | tr "[:blank:]" "_"`
echo mv "$dir/$i" "$dir/$j"
done