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 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
1  
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
add comment

1 Answer

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

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.