removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command?

find Desktop/$dir -name "*.dmg" -print -exec ???

Could use -print0 instead of -print. This helps to print file name that contain spaces in between. If pipping the -print0 output to some other command below is the general syntax.

find . -print0 | xargs -0 COMMAND

This bashscript should do it. Replaces space with undscore (change to any char you want, or remove for space removal).

find . -name "*.dmp" -print | while read file
do
   mv "${file}" "${file// /_}"
done

For other shells you could do.

find . -name "*.dmp" -print | while read file
do
    new=`echo "$file" | sed 's/ /_/g'`
    mv "$file" "$new"
done

If the directory contains files that don't have spaces in them, the earlier commands will generate errors as the source and destination names for the mv command will be the same and mv generally doesn't like that.

The following can be executed in a directory that has files with and without blanks in the name. It will attempt only to move the files with blanks:

find . -name "*dmp"  -exec ksh -c 'x="{}"; [[ $x == *" "* ]] &&  mv "$x" "${x// /}"' \;

This is horrible in terms of efficiency and I would do it the way Chubler_XL suggested with an additional test:

find . -name "*.dmp" -print | while read file
do
   if [[ $file == *" "* ]]
   then
       mv "${file}" "${file// /_}"
   fi
done

@michael, you cannot do that since mv is a rename in this case, which is a one-on-one operation.
@agama,chubler, you need to select only files that contain spaces, otherwise you end up moving files to themselves.
@agama,chubler: that will not work if intermediate directories contain spaces too
bash/ksh93

find . -name "* *.dmg" | 
while read p
do
  f=${p##*/}
  d=${p%/*}
  mv "$d/$f" "$d/${f// /_}"
done

or: (other shells)

find . -name "* *.dmg" | 
while read p
do
  f=${p##*/}
  d=${p%/*}
  mv "$d/$f" "$d/$(echo "$f" | tr ' ' _ )"
done

If you only need to do it in the current directory (bash/ksh93):

for f in *\ *.dmg
do
  mv "$f" "${f// /_}"
done