I've got a whole folders of files with names like:

super_awesome___[stuff_here]_-_is_cool.ext
super_duper_coolness___[more_stuff_here]_-_look_at_me_cool.ext

What's the easiest way, in bash, to rename all of them so the underscores become spaces? There are too many files for me to do this manually.

link|improve this question

feedback

1 Answer

up vote 2 down vote accepted

This will replace multiple _ with one for all files named *.ext

for f in *.ext; do mv $f "$(echo $f | sed 's/_\+/ /g')"; done

if you don't want that remove the \+

for f in *.ext; do mv $f "$(echo $f | sed 's/_/ /g')"; done
link|improve this answer
I should test before posting code... Now it should be correct – Nifle Nov 8 '11 at 18:06
The original would've replaced all underscores and the following character (so lol_this) with spaces (would be lol his), right? – Rob Nov 8 '11 at 18:42
@Rob - you are correct – Nifle Nov 8 '11 at 18:54
2  
mv "$f" "${f//_/ }" – grawity Nov 8 '11 at 19:23
Woah woah woah... how did that work, grawity? I understand the echo | sed answer. Does ${f//_/ } basically mean ${f for all of the $f's, //_/ } substitute _ for space? – Rob Nov 9 '11 at 17:50
show 2 more comments
feedback

Your Answer

 
or
required, but never shown

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