bash 如何 scp 文件名中带有冒号的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14718720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:28:30  来源:igfitidea点击:

How can I scp a file with a colon in the file name?

bashescapingscp

提问by Nathan Jones

I'm trying to copy a file using scpin bashwith a colon (:) character in the source filename. The obfuscated version of my command I'm using is:

我试图复制使用文件scpbash有一个冒号(:源文件名)字符。我正在使用的命令的混淆版本是:

scp file\:\ name.mp4 user@host:"/path/to/dest"

I get this error:

我收到此错误:

ssh: Could not resolve hostname Portal 2: Name or service not known

I know I could just rename the file and remove the :, but I'd like to know if it's possible to escape the colon.

我知道我可以重命名文件并删除:,但我想知道是否可以转义冒号。

回答by mr.spuratic

Not quite a bashescaping problem, it's scptreating x:as a [user@]host prefix, try:

并不完全是bash逃避问题,它的scp治疗x:作为[用户@]主机前缀,请尝试:

scp ./file:\ name.mp4 user@host:"/path/to/dest"

Using relative (e.g. ./) or fully qualified paths (/path/to/source) prevents this behaviour - the presence of /before a :causes OpenSSH to stop checking for a possible host:or user@host:prefix).

使用相对(例如./)或完全限定路径(/path/to/source)可以防止这种行为 -/之前的存在:导致 OpenSSH 停止检查可能的host:user@host:前缀)。

OpenSSH's scponly special-cases filenames that startwith a colon allowing those to work without problems, it has no support for escaping a :in the normal sense, and has no other notion of valid hostnames so almost any filename with a :can cause this (or equivalent IPv6 behaviour if []are found before :).

OpenSSHscp唯一以冒号开头的特殊情况文件名允许它们正常工作,它不支持转义:正常意义上的 a,并且没有其他有效主机名的概念,因此几乎任何带有 a 的文件名:都可能导致这种情况(或等效的如果[]之前发现IPv6 行为:)。

This can also affect other programs, e.g. rsync, the same workaround applies there.

这也会影响其他程序,例如rsync,相同的解决方法适用于那里。

(Due to OpenSSH's simplistic parsing of []enclosed IPv6 addresses, you can successfully scpfiles containing :which start with [, or contain @[before the :and do not contain ]:, but that's not generally useful ;-)

(由于 OpenSSH 对[]封闭 IPv6 地址的简单解析,您可以成功地scp包含:以 开头[或包含@[在 之前:且不包含的文件]:,但这通常没有用 ;-)



(The below text was writtenwhen the original questionwas How do I escape a colon in bash?It applies to that situation, but not to scpas no amount of shell escaping will help there.)

下面的文字是最初的问题如何在 bash 中转义冒号时写的?它适用于这种情况,但不适用于scp任何外壳转义都无济于事。)

To answer the question about howto escape :, you don't need to, but "\:" works. Places that a :is used:

要回答有关如何逃逸的问题:,您不需要回答,但 " \:" 有效。:使用a 的地方:

  1. the null command :, no need to escape, though you can, just like \e\c\h\o fooit has no effect on the command ("no effect" is not completely true, if you escape one or more characters it will prevent an alias being matched, and you canalias :)
  2. PATH(and others, CDPATH, MAILPATH) escaping the values has no useful effect (I have been unable to run a program in my PATH from a directory containing a :, which is a little unexpected)
  3. parameter expansion ${name:-x}and more, namemust be [a-zA-Z_][a-zA-Z0-9_], so no need to escape variables names, and since there's no ambiguity, no need to escape subsequent :in the other variations of parameter expansion
  4. ? :trinary operates only on variables and numbers, no need to escape
  5. ==and =~with classes in the pattern like [[:digit:]], you can escape with \:but I'm at a loss as to how that might ever be useful...
  6. within command or function names, no need to escape, \:has no useful effect
  1. 空命令:,不需要转义,虽然你可以,就像\e\c\h\o foo它对命令没有影响一样(“无效”并不完全正确,如果你转义一个或多个字符,它将阻止别名被匹配,你可以别名:)
  2. PATH(和其他人,CDPATH, MAILPATH)对值进行转义没有任何有用的效果(我一直无法从包含 的目录在我的 PATH 中运行程序:,这有点出乎意料)
  3. 参数扩展${name:-x}等,name必须是[a-zA-Z_][a-zA-Z0-9_],所以不需要转义变量名称,并且由于没有歧义,不需要:在参数扩展的其他变体中转义后续
  4. ? :三元只对变量和数字进行运算,无需转义
  5. ===~模式中的类[[:digit:]],你可以逃避,\:但我不知道这可能有什么用......
  6. 在命令或函数名内,不需要转义,\:没有任何有用的效果

(Note that the null command is just :, you can have a command or function named like ":foo" and it can be invoked without escaping, in this respect it's different to #where a command named #foowould need to be escaped.)

(请注意,空命令只是:,您可以拥有一个名为“ :foo”的命令或函数,并且可以在不转义的情况下调用它,在这方面,它与需要转义#命名的命令的位置不同#foo。)

回答by yelliver

I try using fully qualified paths as @mr.spuratic answer but not work and in my situation, I have to use absolute paths, this is my solution:

我尝试使用完全限定的路径作为@mr.spuratic 答案但不起作用,在我的情况下,我必须使用绝对路径,这是我的解决方案:

scp `hostname`:/root/this/is/test/file.txt user@host:"/path/to/dest"