If you have python installed try this:
Code:
#!/usr/bin/python
# the following script will traverse through all of the files
# within the directory where it is placed and replace each
# space within the filename with an underscore
import os, sys
if len(sys.argv) == 1:
filenames = os.listdir(os.curdir)
else:
filenames = sys.argv[1:]
for filename in filenames:
if ' ' in filename:
newfilename = filename.replace(' ', '_')
print "Renaming", filename, "to", newfilename, "..."
os.rename(filename, newfilename)
place the code in a text file (with a .py extension), make it executable, place it in the directory whose files you wish to change, and either run it from the command line (python name-of-file.py) or just click on it.
If you want to do it in bash, try this:
Code:
for i in *; do mv "$i" "$(echo $i | tr ' ' '_')"; done
or this:
Code:
for i in * *; { mv ''$i'' $(echo $i | sed 's/ /_/g'); }
or this (this one is recursive):
Code:
#!/bin/bash
# Recursive replacement of spaces with "_" in files/dirs
[ $# -ne "1" ] && echo "Usage: $0 <dir-path>" && exit 1
find $1 -name "* *" | sed 's/^.*$/mv \"&\"/g' > t1$$
find $1 -name "* *"|sed -e 's/ /_/g' -e 's/^.*$/\"&\"/g' >t2$$
paste t1$$ t2$$ > t3$$; sh t3$$; #rm -f t?$$
#[t?$$ files will help you in an accidental action]
just cd to the directory and type one of the above. It should work, but I think the python code is a little faster.