Linux — ハイフンで始まるファイルを削除する


解決

ダブルダッシュの後に消したいファイル名を書く。

rm -- -file.txt

意味

ダブルダッシュ ( -- ) はコマンドフラグの終わりを表すらしい。

The double dash “–” means “end of command line flags

UNIX - BASH - What does “--” (double-dash) mean? (also known as “bare double dash”)-Learn in 30 Sec from Microsoft Awarded MVP

問題

-file.txt がコマンドオプションと認識されてしまい、消せなかった。

$ rm -file.txt
rm: illegal option -- l
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

エスケープも不可。

$ rm '\-file.txt'
rm: \-file.txt: No such file or directory