如何在文件名中用冒号scp文件? [英] How can I scp a file with a colon in the file name?

查看:262
本文介绍了如何在文件名中用冒号scp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用冒号( bash 中使用 scp 复制文件>:)字符在源文件名。我使用的我的命令的混淆版本是:

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

我收到这个错误:

  ssh:无法解析主机名Portal 2:名称或服务未知

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

解决方案

不是一个 bash 转义问题,它的 scp x:作为[user @]主机前缀,尝试:

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

使用相对(例如 ./ )或完全限定路径( / path / to / source )阻止这种行为 - / 之前的存在:导致OpenSSH停止检查一个可能的主机: user @ host :前缀)。 OpenSSH的 scp 只有特殊情况下,开始的冒号允许这些工作没有问题的文件名,它不支持转义在正常意义上,没有其他有效的主机名概念,所以几乎任何文件名与可以导致这种情况。 >

(由于OpenSSH简单解析 [] 封闭的IPv6地址,您可以成功 scp 包含的文件: [开头,或包含 @ [ / code>之前的并且不包含]:,但这不是一般有用的;-)






原始问题如何在bash中转义冒号?适用于这种情况,但不适用于 scp ,因为没有数量的shell转义将有助于。)



要回答问题ab out 如何转义,您不需要,但 \:作品。使用的地方:


  1. null命令,不需要逃脱,虽然可以,就像 \e\c\h\o foo 它没有对命令的影响(无效果不完全正确,如果您逃避一个或多个字符,则会阻止别名匹配)

  2. PATH (和其他人, CDPATH MAILPATH )转义值没有任何有用的效果(我已经无法在包含的目录中的程序中运行程序:,这有点意外)

  3. 参数扩展 $ {name:-x} 等等,名称必须是 [a-zA-Z _] [a -zA-Z0-9 _] ,所以不需要转义变量名,因为没有歧义,不需要在后面的中退出:参数扩展的其他变体

  4. ? : trinary仅对变量和数字进行操作,不需要转义

  5. == =〜,类似于 [[:digit:]] 中的类,您可以使用 \\ \\:但是我对于这可能是如何有用的... ... / $>
  6. 在命令或函数名称中,不需要转义, code> \:没有任何效果

(请注意,空命令只是,您可以使用命名或函数名为:foo ,并且可以不转义而调用它这个方面与不同,其中需要转义名为 #foo 的命令。)


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

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.

解决方案

Not quite a bash escaping problem, it's scp treating x: as a [user@]host prefix, try:

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). OpenSSH's scp only special-cases filenames that start with 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.

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


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

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

  1. the null command :, no need to escape, though you can, just like \e\c\h\o foo it 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)
  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, name must 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

(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 #foo would need to be escaped.)

这篇关于如何在文件名中用冒号scp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆