我经常scp
将文件从我的 Android 设备传输到我的 MacBook,这就像一个魅力。但John's folder
我的 MacBook 上有一个名为 \n 的文件夹,因此当我尝试复制该目录中的文件时scp macbook@192.168.0.3:/Users/macbook/desktop/John\\'s\\ folder/file storage/folder
它返回一个错误
\n\nunexpected EOF error while looking for matching \\`\xe2\x80\x99\\`\n
Run Code Online (Sandbox Code Playgroud)\n\n和
\n\nunexpected end of file\n
Run Code Online (Sandbox Code Playgroud)\n\n我该如何解决这个问题?
\n从2022年4月8日发布的OpenSSH 9.0版本开始,该scp
程序现在直接使用SFTP协议。因此,原来的问题不再存在,因此不再需要该答案的其余部分。
如果您使用的是 的旧版本scp
,或者您想使用 恢复到旧的 SCP 协议scp -O
,答案仍然适用。
原始答案如下。
这很有趣。我看到的其他答案是告诉您将转义引号和转义空格交换为带引号的字符串。实际上它们是等效的,因此您不会看到任何变化(a\'\ b
与 shell 相同"a' b"
)。
scp
这里的问题在于远程系统解释所给出的命令行的方式。
举个例子,这可以工作:
scp John\'s\ folder/file localhost:/tmp/dst
Run Code Online (Sandbox Code Playgroud)
但这会失败:
scp localhost:/tmp/src/John\'s\ folder/file /tmp/dst
Run Code Online (Sandbox Code Playgroud)
(我已经用于localhost
示例;您应该user@host
根据您的情况使用。)
如果包含-v
( verbose ) 标志,scp
您可以准确地看到导致失败的原因:
debug1: Sending command: scp -v -f /tmp/src/John's folder/file
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)
这里不幸的解决方案是您需要转义特殊字符(包括空格)两次 - 一次用于本地 shell,一次用于远程 shell:
scp localhost:"/tmp/src/John\'s\ folder/file" /tmp/dst
Run Code Online (Sandbox Code Playgroud)