我想将一个文件从远程系统复制到本地系统。现在我正在使用Linux系统中的scp命令。我的一些文件夹或文件名中包含空格,当我尝试复制这些文件时,它会显示错误信息:“没有这样的文件或目录”。
我尝试过:
scp ael5105@192.168.0.200:'/home/5105/test/gg/Untitled Folder/a/qy.jpg' /var/www/try/
我在网上看到了一些参考资料,但我并不完全理解,请问有人可以帮忙吗?
在复制时如何转义文件名或目录名中的空格...
我想将一个文件从远程系统复制到本地系统。现在我正在使用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 .
scp user@example.com:"'web/tmp/Master File 18 0 13.xls'" .
scp user@example.com:'$(ls -t | head -1)' .
以获取服务器上最近创建的文件,或者 scp user@example.com:'dir/*.{xml,pdf}' .
以从远程目录获取所有的 xml 和 pdf 文件。总的来说,我更喜欢这种方法而不是为具有空格的文件提供方便。带有空格的文件总是很麻烦。 - JoL作品
scp localhost:"f/a\ b\ c" .
scp localhost:'f/a\ b\ c' .
无法工作
scp localhost:'f/a b c' .
这是因为在路径传递到scp命令之前,字符串会被shell解释。因此,当它传递到远程主机时,远程主机将寻找一个具有未转义引号的字符串,从而导致失败。
要验证这一点,请使用-vx选项启动shell,即bash -vx
,它将显示运行命令的插值版本。
"
或单引号 '
的效果相同。 - Vorsprung在目录名称中使用三个反斜杠来转义空格:
scp user@host:/path/to/directory\\\ with\\\ spaces/file ~/Downloads
将复制远程目录中名为directory with spaces
的file
到您的Downloads
目录。
\\\_
被转义一次以得到\_
,然后再次转义以得到一个空格_
。我使用_
来清晰地表示一个空格。 - Ludovic Kuty你也可以像这样做:
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
的错误。具体原因可以在这里详细讨论。source="path with spaces"
printf -v source "%q" "${source}"
scp user@host:"${source}" <destination>
用于Shell使用的一行命令:
source="path with spaces"; printf -v source "%q" "${source}"; scp user@host:"${source}" <destination>
-T
也能正常工作。printf
替代方案的一行代码。 - Fonic我很难让这个命令适用于一个包含空格的文件名的shell变量。由于某种原因,使用以下方式无法解决:
file="foo bar/baz"
scp user@example.com:"'$file'"
像 @Adrian 的答案一样似乎会失败。
事实证明,最好的方法是使用参数扩展来将反斜杠添加到空格前面,具体如下:
file="foo bar/baz"
file=${file// /\\ }
scp user@example.com:"$file"
file="${file//\ /\\\ }"
- troyfolgerfile=${file//\\/\\\\};file=${file// /\\ }
- cayhorstmannexport file='"foo bar/baz"' ; scp user@example.com:"$file"
。这样我就可以避免替换。也许这值得在答案中包含。 - Kjell很抱歉在这个Linux问题中放置Windows 10 Powershell的技巧:在这种情况下,使用反斜杠转义空格或用引号包围空格无效。虽然不是很高效,但我使用"?"字符解决了它:
对于文件"tasks.txt Jun-22.bkp",我使用"tasks.txt?Jun-22.bkp"下载。
scp ael5105@192.168.0.200:/home/5105/test/gg/Untitled?Folder/a/qy.jpg /var/www/try/
? 在远程执行通配符匹配,包括空格。
我是一个新手,使用的是Kali xD操作系统。以下是我的使用方法:如果你想要传输带有单个或多个空格的文件,从你的系统到远程服务器:
$ scp this\ is\ my\ file tryhackme@1.1.1.1:/home/tryhackme
/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/tryhackme'
不是
$ /home/tryhackme