Super User is a question and answer site for computer enthusiasts and power users. 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've got a whole folders of files with names like:


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
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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .