how to delete file with -- in its name

Written at 2024 Sep 05

in notes

114 words

bash shell

If you have a file with name that start with --, remove its isn’t straightforward.

❯ ls -lah
-rw-r--r--    1 vinh  staff     0B Sep  5 17:32 --rm

❯ rm "--rf"
rm: illegal option -- -
usage: rm [-f | -i] [-dIPRrvWx] file ...
       unlink [--] file

❯ rm "\-\-rf"
rm: \-\-rf: No such file or directory

❯ rm '--rf'
rm: illegal option -- -

The trick to delete this file is to use --. After double dash, you can type any argument name and they are evaluate as its, no shell expansion etc.

This is also for case you want to use grep to find pattern that starts with for example.