如何在 Linux 中为 scp 副本转义路径中的空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19858176/
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-08-07 01:17:39  来源:igfitidea点击:

How do I escape spaces in path for scp copy in Linux?

linuxunixubuntuwhitespacescp

提问by AlexPandiyan

I'm new to linux, I want to copy a file from remote to local system... now I'm using scp command in linux system.. I have some folders or files names are with spaces, when I try to copy that file, it shows the error message: "No such file or directory"

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

I tried:

我试过:

scp [email protected]:'/home/5105/test/gg/Untitled Folder/a/qy.jpg' /var/www/try/

I saw the some reference online but I don't understand perfectly, can any one help on this?

我在网上看到了一些参考资料,但我不太明白,有人可以帮忙吗?

how can I escape spaces in file name or directory names during copying...

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

回答by Vorsprung

works

作品

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

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

does not work

不起作用

scp localhost:'f/a b c' .

The reason is that the string is interpreted by the shell before the path is passed to the scp command. So when it gets to the remote the remote is looking for a string with unescaped quotes and it fails

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

To see this in action, start a shell with the -vx options ie bash -vxand it will display the interpolated version of the command as it runs it.

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

回答by Adrian Gunawan

Basically you need to escape it twice, because it's escaped locally and then on the remote end.

基本上你需要转义它两次,因为它在本地转义,然后在远程端转义。

There are a couple of options you can do (in bash):

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

scp [email protected]:"'web/tmp/Master File 18 10 13.xls'" .
scp [email protected]:"web/tmp/Master\ File\ 18\ 10\ 13.xls" .
scp [email protected]:web/tmp/Master\\ File\\ 18\\ 10\\ 13.xls .

回答by MattK

Also you can do something like:

您也可以执行以下操作:

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

The first level of quotes will be interpreted by scp and then the second level of quotes will preserve the spaces.

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

回答by Luke Davis

I had huge difficulty getting this to work for a shell variablecontaining a filename with whitespace. For some reason using

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

file="foo bar/baz"
scp [email protected]:"'$file'"

as in @Adrian's answer seems to fail.

正如@Adrian 的回答似乎失败了。

Turns out that what works best is using a parameter expansion to to prepend backslashes to the whitespace, as follows.

事实证明,最好的方法是使用参数扩展来将反斜杠添加到 whitespace,如下所示。

file="foo bar/baz"
file="${file//\ /\\ }"
scp [email protected]:"$file"

回答by Chris

Use 3 backslashes to escape spaces in names of directories:

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

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

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

should copy to your Downloadsdirectory the filefrom the remote directory called directory with spaces.

应该从名为 的远程目录复制到您的Downloads目录。filedirectory with spaces

回答by Vitor Pepicon

Sorry for using this Linux question to put this tip for Powershell on Windows 10: the space char escaping with backslashes or surrounding with quotes didn't work for me in this case. Not efficient, but I solved it using the "?" char instead:

很抱歉使用这个 Linux 问题将这个提示用于 Windows 10 上的 Powershell:在这种情况下,用反斜杠转义或用引号包围的空格字符对我不起作用。效率不高,但我使用“?”解决了它 字符代替:

for the file "tasks.txt Jun-22.bkp" I downloaded it using "tasks.txt?Jun-22.bkp"

对于文件“tasks.txt Jun-22.bkp”,我使用“tasks.txt?Jun-22.bkp”下载了它