I wanted to replace all hyphens in all filenames starting with 2003- and ending with .php. Here's how I did it using sed (no guarantees, no warranties, use at your own risk!):
- Backup the files!
- Change to the directory you want to make the changes in.
- Then, to make sure the right filenames will be changed, list the filenames using the following sed command (modify "2003-*.php" and the sed search and replace strings for your situation):
for filename in 2003-*.php; do echo $filename | sed \e 's/\-/\_/g'; done
- When you're sure the above sed command is listing the right filenames, change it as follows (modify "2003-*.php" and the sed search and replace strings for your situation):
for filename in 2003-*.php; do newname=`echo $filename | sed \e 's/\-/\_/g'`; mv $filename $newname; done
Note the use of the backtick character (`) (also known as backquote) which, on my keyboard, is located to the left of the "1" key. The backtick character is used in addition to the single quote character (').
[Update January 4, 2009] I totally forgot that I could have used the "rename" command instead of sed! There is control over which underscore in a filename is changed. More about renaming files using "rename".