3

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?

1
  • 1
    y dont you try the rename script? It should be built in already. check out this guide.
    – Karthik T
    Nov 5, 2012 at 7:03

1 Answer 1

1

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.

You must log in to answer this question.

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