Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can't get this to work:

I have hundreds of files that our database developer has foolishly put asterisks in, so that we have files named like this:


I need to recursively (two levels in) search and replace all instances of "*" in all filenames and replace them with an underscore, "_".

I've tried the following but it fails, outputting the usage format for the "mv" command hundreds of times (as if for each file it would have tried to process), like I'd expect if I had invalid syntax, but I can't seem to find what's wrong with it:

for x in `find . -regex '.*/\*.*'` ; do mv $x `echo $x | sed s/\*/_/g` ; done

This is on a Mac (10.4) system, so it's a Darwin environment, if that makes any difference.

Please help!

share|improve this question
I should add that I suspect the filename is coming in unescaped, and that the asterisk that is part of the file name is being interpreted as a wildcard, thus wrecking havoc and pulling in a lot of other files that do not have asterisks in their names originally. I don't know if that's actually true or not--and I've no idea how to work around that--but I'd thought I'd through it out for consideration. – neezer Sep 29 '09 at 1:31
up vote 4 down vote accepted

Put quotes around $x everywhere it appears.

for x in `find . -regex '.*/\*.*'` ; do mv "$x" `echo "$x" | sed s/\*/_/g` ; done
share|improve this answer
Thanks, that did the trick! – neezer Sep 29 '09 at 1:48

You need to escape all the *'s with a leading back slash \

share|improve this answer
You mind being a little more specific? Not sure what you're suggesting beyond what I already said I had tried... – neezer Sep 29 '09 at 1:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.