如何在 Linux 中转义 scp 复制路径中的空格



我想将文件从远程系统复制到本地系统。现在我在 linux 系统中使用 scp 命令。我有一些文件夹或文件名带有空格,当我尝试复制该文件时,它显示错误消息:"没有这样的文件或目录"。

我试过了:

scp ael5105@192.168.0.200:'/home/5105/test/gg/Untitled Folder/a/qy.jpg' /var/www/try/

我在网上看到了一些参考资料,但我并不完全理解,有人可以帮忙吗?

复制过程中如何转义文件名或目录名称中的空格...

基本上你需要

转义它两次,因为它在本地转义,然后在远程端转义。

您可以执行以下几个选项(在 bash 中):

scp user@example.com:"web/tmp/Master File 18 10 13.xls" .
scp user@example.com:web/tmp/Master\ File\ 18\ 10\ 13.xls .

另一个选项仅适用于旧版本的OpenSSH和可能的其他客户端:

scp user@example.com:"'web/tmp/Master File 18 0 13.xls'" .

作品

scp localhost:"f/a b c" .
scp localhost:'f/a b c' .

不起作用

scp localhost:'f/a b c' .

原因是在将路径传递给 scp 命令之前,字符串由 shell 解释。 因此,当它到达遥控器时,遥控器正在寻找带有未转义引号的字符串,但它失败了

要查看此操作,请使用 -vx 选项(即 bash -vx)启动一个 shell,它将在运行时显示命令的插值版本。

使用 3 个反斜杠转义目录名称中的空格:

scp user@host:/path/to/directory\ with\ spaces/file ~/Downloads

应将名为 directory with spaces 的远程目录中的file复制到Downloads目录中。

你也可以做这样的事情:

scp foo@bar:""apath/with spaces in it/""

第一级引号将由 scp 解释,然后第二级引号将保留空格。

尝试使用 Bash 脚本中的scp从包含空格的远程路径复制文件时,我遇到了类似的问题。

以下是我想出的解决方案:

手动逃生路径:

scp user@host:'dir with spaces/file with spaces' <destination>
scp user@host:"dir\ with\ spaces/file\ with\ spaces" <destination>
scp user@host:dir\ with\ spaces/file\ with\ spaces <destination>

注意:不需要选项 -T(见下文)。

使用双引号 + 选项-T

scp -T user@host:"'path with spaces'" <destination>
scp -T user@host:'"path with spaces"' <destination>
scp -T user@host:""path with spaces"" <destination>

注意:如果没有选项 -T,这些命令将失败并显示 protocol error: filename does not match request 。这里详细讨论了其中的原因。

使用 Bash 的 printf 的逃生路径:

source="path with spaces"
printf -v source "%q" "${source}"
scp user@host:"${source}" <destination>

用于外壳的单衬:

source="path with spaces"; printf -v source "%q" "${source}"; scp user@host:"${source}" <destination>

注意:在没有选项 -T 的情况下工作正常。

我很难让它适用于包含带有空格的文件名的 shell 变量。出于某种原因使用:

file="foo bar/baz"
scp user@example.com:"'$file'"

就像@Adrian的回答似乎失败了一样。

事实证明,最有效的方法是使用参数扩展在空格前面附加反斜杠,如下所示:

file="foo bar/baz"
file=${file// /\ }
scp user@example.com:"$file"

很抱歉使用这个 Linux 问题为 Windows 10 上的 Powershell 提供此提示:在这种情况下,用反斜杠或引号括起来的空格字符对我不起作用。效率不高,但我使用 "?" 字符解决了它:

对于文件"任务.txt Jun-22.bkp",我使用"任务.txt下载了它?Jun-22.bkp"

scp ael5105@192.168.0.200:/home/5105/test/gg/Untitled?Folder/a/qy.jpg /var/www/try/

? 在遥控器上执行 glob 操作,并将匹配任何字符,包括空格

我是 Linux 的新手,我使用了 Kali xD,这是我的工作原理: 从您的系统到远程服务器 如果您想传输带有单个或多个空格的文件:

$ scp this is my file tryhackme@1.1.1.1:/home/tryhackme

请注意,IP 地址只是示例,最后一个 I 类型是目录和美元符号,这是提示,代码以 scp 开头。

您还可以指定传输该文件时所需的名称,例如。 /home/tryhackme/file1或具有单个或多个空格,如/home/tryhackme/"this is new file"

从远程服务器到您的系统,与上面相同,具有单个或多个空格:

$ scp tryhackme@1.1.1.1:/home/tryhackme/"remote server file" "this is mine now"

请注意,您只对要复制的文件使用反斜杠\,因为您可以看到字符串"this is mine now"没有\,因为这是我们传输到系统时想要的文件的名称,而不是我们要保护复制的文件 (scp)。对不起我的解释,我希望有人能理解,我希望这对需要另一个解决方案的人有所帮助。

如果文件或文件夹名称之间有空格,那么您只需在空格前添加一个黑色斜杠 '',然后将整个路径放在一个引号 ('') 中,然后它应该可以工作。

例:

假设文件夹名称为"测试文件夹",并且位于远程计算机的/home/内。然后,您可以使用以下 scp 命令访问或下载该文件夹。

scp -r <user>@<host>:'/home/Test Folder' .

在 linux-terminal 或 cmd 中,如果单词之间的路径中有任何空格,则必须使用引号 ('') (") 标记。

你应该像这样:

$ '/home/tryhackme'

$ /home/tryhackme

只需在空格应该在的地方贴通配符:"foo/bar*bar.txt"

尝试以下操作

scp ael5105@192.168.0.200:"/home/5105/test/gg/Untitled Folder/a/qy.jpg" /var/www/try/

在这里,文件夹前的空间用"\"(反斜杠)转义,路径放在"(引号)下。也请检查/var/www/try/是否存在。

最新更新