Did you know?
To delete a file whose name starts with a dash, use rm -- -file.
Without --, rm reads the name as an option and fails.
- Files & directories
- Last checked
Explained
A file named -file.txt or --help is easy to create by accident, for example with a typo in a redirect, and surprisingly hard to delete. rm treats anything that starts with a dash as an option:
rm -file.txt
rm: invalid option -- 'l'
Try 'rm ./-file.txt' to remove the file '-file.txt'.
Two ways around it. A double dash marks the end of the options, so everything after it is a file name:
rm -- -file.txt
Or give a path, so the name no longer starts with a dash:
rm ./-file.txt
The -- convention works with almost every command-line tool: cat -- -notes, mv -- -a b, grep -- -v file (search for the text -v). In scripts, put -- before variables that hold file names, as in rm -- "$file", so an unexpected name can never be read as an option.
Learn more
- How to Remove Files and Directories in Linux Full guide · Updated Apr 2025
-
rmcommand cheat sheet Options and copy-ready examples
