How Do You Troubleshoot the Error “Invalid Option” When Trying to Delete a File That Has a File Name with the First Character Being a Hyphen or Dash “-“?

Problem scenario
You have some files that have a name that starts with a hyphen (e.g., from some Bash script that did not work as intended):

-the
-filename
-q

You cannot delete them using rm.  You get an error about an "invalid option".  What should you do to delete this files that should never have been created in the first place?

Solution
Use the rm flag "--" such as this:

rm -- -the
rm -- -filename
rm -- -q

Leave a comment

Your email address will not be published. Required fields are marked *