Delete a file starting with a dash/hypen on Linux on the command line
Posted July 6th, 2008 in Linux/Unix/BSD
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 210.5.53.35.log -rw-r--r-- 1 root root 18114 Apr 30 01:37 210.5.53.36.log -rw-r--r-- 1 root root 18410 Apr 30 01:37 210.5.53.37.log ...
You try to delete the -.log file like this:
rm -.log
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:
rm ./-.log
This will also work for other command line utilities you need to run against a file starting with a hypen/dash.
Subscribe!
If you found this post interesting and would like to be notified the next time something is posted, please subscribe to my RSS Feed. Thanks for visiting!
