So you have a file that starts with a dash/hyphen/- and need to delete it. But when you try to do so, the "rm" command complains that you have passed an "invalid option –". This post looks at the simple solution to deleting a file that starts with a dash/hyphen.
As an example, doing a directory listing gives you something like this:
$ ls -l total 678586 -rw-r--r-- 1 root root 54 Apr 29 15:39 -.log -rw-r--r-- 1 root root 26819 Apr 30 13:17 188.8.131.52.log -rw-r--r-- 1 root root 18114 Apr 30 01:37 184.108.40.206.log -rw-r--r-- 1 root root 18410 Apr 30 01:37 220.127.116.11.log ...
You try to delete the -.log file like this:
and rm complains like this:
rm: invalid option -- . Try `rm ./-.log' to remove the file `-.log'. Try `rm --help' for more information.
The answer is actually supplied in the error message, which is fairly unusual for a Linux/Unix command. Instead of doing rm -.log you simply add ./ to the start and run this instead:
This will also work for other command line utilities you need to run against a file starting with a hypen/dash.