[Solved] rm: invalid option -- ' ' in Linux

rm invalid option Linux terminal error

When working on Linux or Unix-like systems, you may occasionally run into this frustrating error:

rm: invalid option -- ' '

This guide explains why this happens and how to fix it quickly.

Why Does This Error Occur?

The rm command is used to remove files and directories from the filesystem. It interprets any argument starting with a hyphen (-) as an option flag, not a filename.

So when a filename starts with a hyphen — for example -filename — the rm command gets confused and throws the invalid option error.

This commonly happens when:

  • Files are created accidentally with a leading hyphen
  • Scripts generate filenames with special characters
  • You copy-paste a filename that starts with -

How to Reproduce the Error

Try creating and removing a file with a hyphen:

# Create a test file with hyphen in name
touch -- -testfile

# Try to remove it normally
rm -rf -testfile

You will see:

rm: invalid option -- 't'

The Fix — Use Double Dash

Use -- (double dash) before the filename. This tells rm that everything after -- is a filename, not an option:

rm -rf -- -testfile

The -- acts as a delimiter — it signals the end of options and the beginning of filenames, even if those filenames start with a hyphen.

Alternative Methods

Method 1 — Use the Full Path

rm -rf ./-testfile

Prefixing with ./ forces rm to treat it as a path, not an option.

Method 2 — Use the find Command

find . -name "-testfile" -delete

Useful when you have multiple hyphen-prefixed files to delete at once.

Quick Summary

Method Command
Double dash rm -rf -- -filename
Full path rm -rf ./-filename
Using find find . -name "-filename" -delete

All three methods work — rm -- -filename is the most commonly used and easiest to remember.

Comments