I'm trying to rename a file with a hyphen at the beginning of its name and both this:
mv -example-file-name example-file-name
mv '-example-file-name' example-file-name
mv: invalid option -- 'e'
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
Most GNU/Linux commands allow a
-- option to indicate end of options so that subsequent
- prefixed words are not treated as options.
mv -- -example-file-name example-file-name
A small test
$ touch -- -example $ ls -l -- *ample -rw-r--r-- 1 rgb rgb 0 Nov 25 09:57 -example $ mv -- -example example $
This trick works for me in times of desperation. YMMV
rename \- '' *
You have to escape the hyphen for rename to recognize it. Why rename doesn't respect single quotes or offer an override of some kind is beyond me.
This is the only method I've seen that reliably handles a leading hyphen using rename. I agree with the other posts on using mv, but if you can't use mv for any reason, this works.