3

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:

*1*_Floorplan.jpg

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!

voretaq7
  • 79,345
  • 17
  • 128
  • 213
neezer
  • 790
  • 3
  • 10
  • 28
  • 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 01:31

2 Answers2

4

Put quotes around $x everywhere it appears.

for x in `find . -regex '.*/\*.*'` ; do mv "$x" `echo "$x" | sed s/\*/_/g` ; done
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
0

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

Josh Budde
  • 2,378
  • 14
  • 7
  • 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 01:46