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 ael5105@192.168.0.200:'/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...
如何在复制过程中转义文件名或目录名中的空格?
4 个解决方案
#1
339
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 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\\\ 10\\\ 13.xls .
#2
53
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 -vx
and it will display the interpolated version of the command as it runs it.
要查看实际操作,请使用-vx选项ie bash -vx启动shell,它将在运行该命令时显示该命令的内插版本。
#3
17
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解释,第二级引号将保留空格。
#4
1
I had huge difficulty getting this to work for a shell variable containing a filename with whitespace. Turns out that using
对于包含有空格的文件名的shell变量,我很难实现这一点。结果表明,使用
file="foo bar/baz"
scp user@example.com:"'$file'"
as in @Adrian's answer seems to fail (try entering set -x
before the above commands to see how the shell interprets this string; it is pretty wonky and I do not really understand why it fails).
在@Adrian的答案中似乎失败了(尝试在上面的命令之前输入set -x,看看shell如何解释这个字符串;它很不稳定,我也不明白为什么它会失败)。
Turns out that what works best is using a parameter expansion to to prepend backslashes to the whitespace, as follows.
结果表明,最有效的方法是使用参数展开来将空格的前斜杠改为空格,如下所示。
file="foo bar/baz" # a file inside a directory-name with whitespace
file="${file//\ /\\\ }" # the `//` replaces all instances; `/` just replaces the first
scp user@example.com:"$file"
#1
339
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 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\\\ 10\\\ 13.xls .
#2
53
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 -vx
and it will display the interpolated version of the command as it runs it.
要查看实际操作,请使用-vx选项ie bash -vx启动shell,它将在运行该命令时显示该命令的内插版本。
#3
17
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解释,第二级引号将保留空格。
#4
1
I had huge difficulty getting this to work for a shell variable containing a filename with whitespace. Turns out that using
对于包含有空格的文件名的shell变量,我很难实现这一点。结果表明,使用
file="foo bar/baz"
scp user@example.com:"'$file'"
as in @Adrian's answer seems to fail (try entering set -x
before the above commands to see how the shell interprets this string; it is pretty wonky and I do not really understand why it fails).
在@Adrian的答案中似乎失败了(尝试在上面的命令之前输入set -x,看看shell如何解释这个字符串;它很不稳定,我也不明白为什么它会失败)。
Turns out that what works best is using a parameter expansion to to prepend backslashes to the whitespace, as follows.
结果表明,最有效的方法是使用参数展开来将空格的前斜杠改为空格,如下所示。
file="foo bar/baz" # a file inside a directory-name with whitespace
file="${file//\ /\\\ }" # the `//` replaces all instances; `/` just replaces the first
scp user@example.com:"$file"