22

Ok, I have a bunch of files starting with a dash, -. Which is not so good... and I want to rename them. In my particular case I would just like to put a character in front of them.

I found the following line that should work, but because of it dash it doesn't:

for file in -N*.ext; do mv $file x$file; done

If I put an echo in front of the mv I get a bunch of

mv -N1.ext x-f1.ext
mv -N2.ext x-f2.ext

Which is correct, except of course it will think the first filename is options. So when I remove the echo and run it I just get a bunch of

mv: illegal option -- N

I have tried to change it to

for file in -N*.ext; do mv "$file" "x$file"; done

but the quotes are just ignored it seems. Tried to use single quotes, but then the variable wasn't expanded... What do I do here?

Update: I have now also tried to quote the quotes. Like this:

for file in -N*.ext; do mv '"'$file'"' '"'x$file'"'; done

And when I echo that, it looks correct, but when I actually run it I just get

mv: rename "-N1.ext" to "x-n1.ext":: No such file or directory

I have just no clue how to do this now... sigh

Jens Erat
  • 17,507
  • 14
  • 61
  • 74
Svish
  • 38,310
  • 61
  • 136
  • 181
  • [How to remove a file with name starting with “-r” using cli](https://superuser.com/q/689825/241386), [How to open files with forward dash in linux](https://superuser.com/q/603792/241386), [Can't rename a file the name of which starts with a hyphen](https://superuser.com/q/510337/241386), [How do I delete a file whose name begins with “-” (hyphen a.k.a. dash or minus)?](https://unix.stackexchange.com/q/1519/44425) – phuclv Aug 20 '18 at 05:41

2 Answers2

34

You need to use the keyword -- to tell the mv command that the arguments are not to be interpreted as options. Watch:

$ mv -N1.ext x-f1.ext
mv: invalid option -- N
Try `mv --help' for more information.

$ mv -- -N1.ext x-f1.ext
$ ls
x-f1.ext

Use -- after all the options on the commandline. Eg, if you're trying to use the -i option to mv, it would go before --:

mv -i -- -filename-begins-with-dash newfilename
quack quixote
  • 42,186
  • 14
  • 105
  • 129
  • 7
    Aaah. Didn't think about that one at all. In the mean time I found out I could stick `./` in front of the filename as well, which would work. But this is of course an even neater option :) – Svish Mar 15 '10 at 10:53
  • @Svish: that's fair, i didn't think about prepending the filename with `./` either. but you're correct, that works too, for this situation. – quack quixote Mar 15 '10 at 11:10
  • 7
    Note that `--` meaning don't process further arguments as options is a convention, and is not respected by all programs. Some use a lone `-` for that, and some don't accepting *anything* for that purpose. So prepending `./` is worth keeping in mind. – dmckee --- ex-moderator kitten Mar 15 '10 at 19:01
  • @dmckee is right; `--` as a special argument is a GNU extension, but it is mentioned in the POSIX guidelines. see section 12.2, guideline 10: http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html ... this means you can expect most POSIX and GNU utilities to respect the convention, but others may not. – quack quixote Mar 16 '10 at 02:19
7

Another technique is to include the parent directory with the file name...

To rename the file -file-to-rename to file-to-rename...

mv  ../parent-dir/-file-to-rename  file-to-rename

Credit to @Skippy le Grand Gourou for confirming that ./-file-to-rename works with mv and rm as in...

mv  ./-file-to-rename  ./--file-to-rename
    
rm  ./-file-to-rename
bad_coder
  • 643
  • 1
  • 7
  • 16
DocSalvager
  • 320
  • 3
  • 7