頭に - (ハイフン)を付けたファイルの削除方法

なんかの拍子に作成してしまい、有識者に助けられたのでメモしておく

状況としてはこんな感じ

※以下環境は mac

localhost:test hoge$ ls
-kesitai_file	futu_file

普通に rm で消してみると

localhost:test hoge$ rm -kesitai_file 
rm: illegal option -- k
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

引数に認識されちゃって消せない

点々とか付ければいけるんじゃね、と思っても

localhost:test hoge$ rm "-kesitai_file" 
rm: illegal option -- k
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file
localhost:test hoge$ rm '-kesitai_file' 
rm: illegal option -- k
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

shell に渡される段階でクオーテーションが展開(外れる)されるので 結果変わらず

じゃあどうするか

localhost:test hoge$ rm ./-kesitai_file 
localhost:test hoge$ ls
futu_file

パスをちゃんと指定すれば消える、確かに消えた。オレの 10 分と焦りを返して欲しい。

ちなみに手元の CentOS 5.4 で試してみると

[hoge@localhost ~]$ rm -machigai_file 
rm: invalid option -- m
Try `rm ./-machigai_file' to remove the file `-machigai_file'.
Try `rm --help' for more information.

`rm ./-machigai_file' を試せと警告してくれる。仕事中は焦ってこの警告が目に入らなかったのかなぁ。。