Tuesday, February 14, 2006

unix tip: rename files - replace spaces with underscores

last week a friend of mine asked me how to rename a bunch of files that have spaces on its name.
this is what i came up with:
$ cat replace_spaces_with_underscores.sh
#!/bin/ksh
while read filenames
do
mv "$filenames" "`echo "$filenames"sed -e 's/ /_/g'`"
# use below line if you don't want to replace it with underscore
#mv "$filenames" "`echo "$filenames"sed -e 's/ //g'`"
done < filenames_with_spaces

but 1st u need to run this, dump filenames with spaces to one file:
$ ls grep " " > filenames_with_spaces

p/s: since i'm in nederland, maybe i should try their space cake :)

No comments: