Deleting files with rm and getting

rm: invalid option —Try ‘rm —help’ for more information.

In Linux, trying to delete a file its name starts with dash(’-’) or double dashes(’—’) will fails, as dash or ’-’ is interpreted as an option by the rm and most linux commands.

Here are two files with filenames starting with ’-’ and ’—’ and the typical rm command deletion attempt fails with an error -

$ ls -1
-tempfile1
--tempfile2

$ rm -tempfile1
rm: invalid option -- 't'
Try 'rm ./-tempfile1' to remove the file '-tempfile1'.
Try 'rm --help' for more information.

$ rm --tempfile2
rm: unrecognized option '--tempfile2'
Try 'rm ./--tempfile2' to remove the file '--tempfile2'.
Try 'rm --help' for more information.

There are several ways of addressing this - you can precede the file name with ./ OR pass double dash after rm to end all option processing.

$ ls
-tempfile1  --tempfile2

$ rm -- -tempfile1

$ mv -- --tempfile2 tempfile2

$ rm ./-tempfile1

References -

http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#How-do-I-remove-files-that-start-with-a-dash_003f