Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 wanted to rename a lot of files with a pattern so I tried this for loop:

$ for f in *; do mv \""$f"\" \""HouseMD-S06E${f#*Episode }"\"; done

But I got this error:

mv: cannot stat `"House MD Season 6 Episode 01 - Broken (Parts 1 & 2).avi"': No such file or directory

So what I did was echo the mv commands to a file like this:

$ for f in *; do echo mv \""$f"\" \""HouseMD-S06E${f#*Episode }"\">>mv.txt; done

And the run the file with source.

Any ideas why the first for didn't work and how can I fix it?

share|improve this question
y dont you try the rename script? It should be built in already. check out this guide. – Karthik T Nov 5 '12 at 7:03
up vote 1 down vote accepted

There's no need for escaping double quotes again. If you use double quotes, this will make sure spaces in the filenames are no issue, even when the variable $f is expanded.

for f in *; do mv "$f" "HouseMD-S06E${f#*Episode}"; done

If you have these "double double quotes", let's assume you have the file foo, then the shell will try to run the command:

mv ""foo"" …

And obviously, "foo" does not exist – only foo – and this is why it fails.

share|improve this answer

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.