简体   繁体   English

为什么命令行 rm 不接受带有空格的目录的引号?

[英]why does command line rm not accept quotation marks for directories with spaces?

Running rm projects/artwork/My Project (543893)/Images/*.png at the command line in Debian does not work because of the spaces and the parenthesis.由于空格和括号,在 Debian 的命令行中运行rm projects/artwork/My Project (543893)/Images/*.png不起作用。 I need to escape them.我需要逃离他们。 But I thought quotation marks "" were an alternative to escaping.但我认为引号“”是 escaping 的替代品。 It works with other commands, such as cd , but not with rm .它适用于其他命令,例如cd ,但不适用于rm Why is that?这是为什么?

Because globbing does not work with quoting:因为 globbing 不适用于引用:

$ ll /tmp/"d i r"/*.png
-rw-r--r--. 1 postgres postgres 0 May 26 14:02 /tmp/d i r/a.png
-rw-r--r--. 1 postgres postgres 0 May 26 14:02 /tmp/d i r/p.png
$ ll "/tmp/d i r/*.png"
ls: cannot access /tmp/d i r/*.png: No such file or directory
$ rm "/tmp/d i r/*.png"
rm: cannot remove ‘/tmp/d i r/*.png’: No such file or directory
$ rm /tmp/"d i r"/*.png
$ ll /tmp/"d i r"/*.png
ls: cannot access /tmp/d i r/*.png: No such file or directory

You should ensure that:您应确保:

  • the spaces and parentheses are inside the quotation marks, so that they are treated as literals, because otherwise these characters will have special meanings for the shell (spaces to separate command-line arguments, and parentheses for a subshell command)空格和括号引号内,因此它们被视为文字,否则这些字符对于 shell 将具有特殊含义(用于分隔命令行 arguments 的空格和用于子 shell 命令的括号)

  • but for the * you want the exact opposite: it must be outside the quotation marks so that it is indeed given its special meaning as a wildcard, not a literal * .但是对于*你想要完全相反:它必须在引号之外,以便它确实被赋予它作为通配符的特殊含义,而不是文字*

Remember that you do not need to apply quotation marks to the whole string, only the part that needs treating as a literal and otherwise would be given a special meaning by the shell.请记住,您不需要将引号应用于整个字符串,只需将需要视为文字的部分,否则将由 shell 赋予特殊含义。

So for example you could use:因此,例如,您可以使用:

rm projects/artwork/"My Project (543893)"/Images/*.png

Similarly, if you use escapes, escape the spaces and parentheses, but not the wildcard:同样,如果您使用转义,请转义空格和括号,但不要转义通配符:

rm projects/artwork/My\ Project\ \(543893\)/Images/*.png

(In other words, you would not use \* .) (换句话说,你不会使用\* 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM