得之我幸 失之我命

when someone abandons you,it is him that gets loss because he lost someone who truly loves him but you just lost one who doesn’t love you.

记一次差点删不掉

想要打包一个文件夹,本意是想用 tar 的 -C 参数处理文件路径,结果操作失误,忘记写了压缩文件名,生成了 ‘-C’ 的压缩文件

1
2
3
$ tar -czvf -C test/
$ ls
-C

然后就开始了一场闹剧,动用了各种大型武器,都不行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ rm -C
rm: invalid option -- 'C'
Try 'rm ./-C' to remove the file ‘-C’.
Try 'rm --help' for more information.

$ rm '-C'
rm: invalid option -- 'C'
Try 'rm ./-C' to remove the file ‘-C’.
Try 'rm --help' for more information.

$ rm '\-C'
rm: cannot remove ‘\\-C’: No such file or directory

$ rm "\-C"
rm: cannot remove ‘\\-C’: No such file or directory

$ rm -f -C
rm: invalid option -- 'C'
Try 'rm ./-C' to remove the file ‘-C’.
Try 'rm --help' for more information.

$ mv '-C' test
mv: invalid option -- 'C'
Try 'mv --help' for more information.

$ mv '\-C' test
mv: cannot stat ‘\\-C’: No such file or directory

完犊子了,删不掉了,这文件怕是要永流传了,心里一颤,祭出 google,搜索 “linux 删除名为 -C 的文件”,终于在知乎里找到了可以用的答案

linux 文件名命名规则

Linux 系统下的文件名长度最多可到 256 个字符。通常情况下,文件名的字符包括:字母、数字、“.”(点)、“_”(下划线)和"-"(连字符)。Linux 允许在文件名中使用除上述符号之外的其它符号, 但并不建议用户这样做

在 Linux 下可以使用长文件名和目录名,可以给它们取任何名字,但是必须遵守以下规则:

  1. 除了/外,所有的字符都可以;
  2. 有些字符最好不用,如空格符,制表符,退格符和字符:? @ # $&()|; ‘’“”<>等;
  3. 避免使用+ -或.作为普通文件名的第一个字符(在Linux下以.开头的文件是属于隐藏文件);
  4. 另外和Windows文件命名不一样的是,在Linux下文件名大小写敏感。

所以要怎么删

rm ./-C

在 Linux 中,通常使用 - 开头,后面是跟一些字符作为命令选项。而不巧的是,-C 会当做被 rm 命令的一个参数选项,实际上并没有该选项,所以会报错,其实上面的提示信息已经给出了解决办法,尝试使用 rm ./-C,或者使用 rm --help 获取更多信息

为什么 rm ./-C 就可以删除了呢?因为文件名前的 ./ 可以避免文件名把的首字符 - 被命令解释器当作 rm 命令的选项

rm – -C

从 rm --help 里可以发现 rm – -C 也可以删除

be yourself, everyone else is already taken.