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

LinuxUnixUbuntuWhitespaceScp

Linux Problem Overview


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"

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...

Linux Solutions


Solution 1 - Linux

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):

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 .

Solution 2 - Linux

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

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.

Solution 3 - Linux

Use 3 backslashes to escape spaces in names of directories:

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

should copy to your Downloads directory the file from the remote directory called directory with spaces.

Solution 4 - Linux

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.

Solution 5 - Linux

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

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

as in @Adrian's answer seems to fail.

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

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

Solution 6 - Linux

I encountered similar issues when trying to copy files from remote paths containing spaces using scp from within a Bash script.

Here are the solutions I came up with:

Escape paths manually:

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>

Note: does not require option -T (see below).

Use double-quoting + option -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>

Note: without option -T, these commands fail with protocol error: filename does not match request. The reason for this is discussed in detail here.

Escape path using printf:

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

One-liner for shell use:

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

Note: works fine without option -T.

Solution 7 - Linux

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:

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

Solution 8 - Linux

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

the ? does a glob on the remote and will match any character, including a space

Solution 9 - Linux

I'm new to Linux and I used Kali xD, here how's mine works: From your system to remote server if you want to transfer your file with single or multiple spaces:

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

Note that the IP address is only example and the last I type is directory and the dollar sign which is the prompt, the code start with scp.

Also you can specifiy what name you want to that file when it is transfer, ex. /home/tryhackme/file1 or with single or multiple spaces like /home/tryhackme/"this\ is\ new\ file".

From remote server to your system, same as above with single or multiple spaces:

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

Note that you only use backward slash \\ to the file you want to copy, as you can see that the string "this is mine now" has no \\ because that is the name of the file we want when it transfer to our system and not the file that we want to secure copy (scp). Sorry for my explanation, I hope someone will understand, I hope this helps to someone who needs another solution.

Solution 10 - Linux

If the file or folder name is having space in between then you can simply add a black slash '' before the space and then put the whole path inside a single quotation ('') and it should work then.

Example:

Suppose the folder name is 'Test Folder' and it is inside /home/ in remote machine. Then you can access or download the folder using following scp command.

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

Solution 11 - Linux

In linux-terminal or cmd if there is any space in path between word you must use quotation ('') ("") mark.

you should something like this :

$ '/home/tryhackme'

not

$ /home/tryhackme

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexPandiyanView Question on Stackoverflow
Solution 1 - LinuxAdrian GunawanView Answer on Stackoverflow
Solution 2 - LinuxVorsprungView Answer on Stackoverflow
Solution 3 - LinuxChrisView Answer on Stackoverflow
Solution 4 - LinuxMattKView Answer on Stackoverflow
Solution 5 - LinuxLuke DavisView Answer on Stackoverflow
Solution 6 - LinuxMaxximView Answer on Stackoverflow
Solution 7 - LinuxVitor PepiconView Answer on Stackoverflow
Solution 8 - LinuxJayenView Answer on Stackoverflow
Solution 9 - LinuxYes But NoView Answer on Stackoverflow
Solution 10 - LinuxRamyaniView Answer on Stackoverflow
Solution 11 - LinuxAhmed AbdallahView Answer on Stackoverflow