在linux中用“-r”删除文件名

Nik*_*val 3 linux

我无法删除名为-rwith的文件名rm -rf -r 是否有任何特殊技巧可以删除它?

小智 16

在 Linux 或类 Unix 系统中,您可能会遇到带有特殊字符的文件名,例如:

-
--
;
&
$
?
*
White spaces, backslashes and more.
Run Code Online (Sandbox Code Playgroud)

问题和解决方案

提示 #1:尝试在文件名开头使用 ./

删除名为“-file”的文件的语法如下:

$ rm -v ./-file

删除了`./-file'

提示 #2:尝试在文件名的开头使用 --

A -- signals the end of options and disables further option processing     by   shell. 

Any arguments after the -- are treated as filenames and arguments.
Run Code Online (Sandbox Code Playgroud)

$ rm ---文件

$ rm -- -- 文件

$ rm -- '@#$%^&file'

$ rmdir -- '--dirnameHere'

  • 只是为了清楚起见:`--` 当然不应该在文件名的开头,而是之前作为一个单独的参数。例子是对的,当然,只有粗体描述令人困惑。 (5认同)
  • 我会用单引号替换双引号,以避免让 shell 尝试解释 $ 和 * 之类的内容。总的来说,这是一个好习惯。我已经提交了一个这样的编辑。 (2认同)

fak*_*ken 12

rm手册页所述:

要删除名称以“-”开头的文件,例如“-foo”,请使用以下命令之一:

      rm -- -foo
      rm ./-foo
Run Code Online (Sandbox Code Playgroud)

所以,在你的情况下:

rm -- -r
Run Code Online (Sandbox Code Playgroud)

或者

rm ./-r
Run Code Online (Sandbox Code Playgroud)