如何删除以“>”或其他异常字符开头的文件

Mic*_*ant 8 rm files

我不小心创建了一个名为

> option[value='2016']
Run Code Online (Sandbox Code Playgroud)

我怎样才能删除它?

My attempts:

$ rm "> option[value='2016']"
rm: cannot remove ‘> option[value='2016']’: No such file or directory
$ rm \> o*
rm: cannot remove ‘>’: No such file or directory
rm: cannot remove ‘o*’: No such file or directory
$ rm `> o*`                                                                               
rm: missing operand
Try 'rm --help' for more information.
$ rm \> option*
rm: cannot remove ‘>’: No such file or directory
rm: cannot remove ‘option*’: No such file or directory
$ rm '\> option*'                                                                         
rm: cannot remove ‘\\> option*’: No such file or directory
$
$ rm "\> option*"                                                                         
rm: cannot remove ‘\\> option*’: No such file or directory
Run Code Online (Sandbox Code Playgroud)

文件清单:

HAPPY_PLUS_OPTIONS/
o*
op*
> option[value='2016']
> option[value='ALFA ROMEO']
README.md
rspec_conversions/
.rubocop.yml
SAD/
SAD_PLUS_OPTIONS/
Run Code Online (Sandbox Code Playgroud)

Arc*_*mar 16

另外一个选项

ls -i 
Run Code Online (Sandbox Code Playgroud)

其中给出(具有适当的 inode 值)

5233 > option[value='2016']   5689 foo
Run Code Online (Sandbox Code Playgroud)

然后

find . -inum 5233 -delete
Run Code Online (Sandbox Code Playgroud)

可选(预览)

find . -inum 5233 -print
Run Code Online (Sandbox Code Playgroud)

-xdev如果下面有另一个文件系统,您还可以添加。


小智 9

您还可以使用“--”选项,根据 man 的说法:

 The rm command uses getopt(3) to parse its arguments, which allows it to
 accept the `--' option which will cause it to stop processing flag options at
 that point.  This will allow the removal of file names that begin with a dash
 (`-').  For example:
       rm -- -filename
Run Code Online (Sandbox Code Playgroud)

所以我试过:

touch -- "> option[value='2016']"
Run Code Online (Sandbox Code Playgroud)

并删除它:

rm -- "> option[value='2016']"
Run Code Online (Sandbox Code Playgroud)

检查文件名是否正确输入的最简单方法:

rm -- ">[tab]
Run Code Online (Sandbox Code Playgroud)

让自动完成来完成这项工作。

PS:尽管听起来很诱人,但不要创建文件名“-rf *”。可能会发生不好的事情。

-rw-r--r--    1 stephan  staff      0 Sep 13 14:11 -rf *
Run Code Online (Sandbox Code Playgroud)

始终使用“-i”以确保安全。

iMac:~ stephan$ rm -i -- "-rf *"
remove -rf *? Y
Run Code Online (Sandbox Code Playgroud)


Mic*_*ant 8

最初的问题是领先的空间,因此

rm " > option[value='2016']"
    ^ here
Run Code Online (Sandbox Code Playgroud)

作品。

更新了关于以 > 等开头的文件的问题。