Last Updated: February 25, 2016
· gnrfan

Moving or renaming a UNIX file starting with a dash

If you happen to create or rename a UNIX file system so it starts with a dash you'll get in a bit of trouble because most commands will get confused and will try to interpret it as an optional switch.

I had a file named "-h" and I was getting errors like this:

mv -h something_else
mv: illegal option -- h
usage: mv [-f | -i | -n] [-v] source target
  mv [-f | -i | -n] [-v] source ... directory

The solution is pretty simple, just prepend "./" to the filename to indicate that the file is in the current directory or alternatively use the full path or a relative path to the file so syntactically it ends up being different to a command switch and you're done.

mv ./-h something_else

It works! :)

1 Response
Add your response

I ran into this before and it took me an embarrassingly long time to figure out. This is really helpful. Nice post!

over 1 year ago ·