Here are couple of scripts I use for this task:
#!/bin/ksh
# Name : unspace - replace spaces by underscores in file names
# Usage : unspace [file ...]
# Example : unspace *.doc
unspace()
{
ls "$@" | while a=$(line)
do
file=$(echo $a | grep " ")
if [ -n "$file" ]
then
file="$(print "$file" | sed 's/ /_/g')"
print "$a" "->" "$file"
mv "$a" "$file"
fi
done
}
[[ "$(basename $0)" = unspace ]] && unspace "$@"
The following one is recursively fixing all names under the current directory. Note that it still need some work if directory names contain embedded spaces too.
#!/bin/ksh
find . |
while a=$(line)
do
newName="$(print $a | tr ' ' '_')"
if [ "$a" != "$newName" ]
then
mv "$a" "$newName"
print $a moved
else
print $a unchanged
fi
done
I suspect an X-Y problem here. Could you tell us why you need to replace those spaces at all? – geek – 2010-04-13T16:37:18.497