Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 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
share|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
3  
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

Your Answer

 
discard

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.