8

How can I rename files with "-" in front of the filename, for example: "-0001.jpg"

Everyime I try to run:

for i in *; do mv "$i" "${i//-/}"; done

or:

for i in *; do mv "$i" "${i#*-}"; done

I got this error:

mv: invalid option -- '0'
Try `mv --help' for more information.

Thanks for any light!

Roger
  • 8,286
  • 17
  • 59
  • 77

3 Answers3

30
mv ./-00008.jpg to/some/where.jpg
   ^ - start with path...
clt60
  • 62,119
  • 17
  • 107
  • 194
  • 3
    While many programs support `--`, this solution will work for absolutely all programs. – hlovdal Jul 02 '11 at 16:12
  • yeap, I am a bit GNU biased since when I had to use non-GNU "common" tools (mv, find, one of non-GNU awk...) on e.g. Solaris, I realized that I missed a lot GNU "extension" and "enhancement". – ShinTakezou Jul 02 '11 at 16:16
  • (no `mv "-hey" dest` wont work, I think since the "" are used before to split arguments into argv array properly, but once placed there, there's no way to know if there where "" or not) – ShinTakezou Jul 02 '11 at 16:38
13

As with most GNU commands, use the -- switch before the filename with the hyphen. It signifies "end of switches".

mv OPTIONS -- -file_with_hyphen.txt dest.txt
meles
  • 315
  • 1
  • 4
  • 13
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
10

Put a double - before the arguments that can contain "-" in the begin; then there can't be options after --.

mv OPTIONS -- ...
ShinTakezou
  • 9,432
  • 1
  • 29
  • 39