Transfer Files and Directories Containing Spaces Using scp

Sunday, December 6, 2015

Have you ever used scp to transfer files and/or directories containing spaces only to have it output the following errors:

[james@raxus] scp -r james@backup.example.com:/home/james/Pictures/London Trip/First Day .
scp: /home/james/Pictures/London: No such file or directory
scp: Trip/First: No such file or directory
scp: Day: No such file or directory

It’s rather annoying.

Your first thought is probably to escape the spaces in the file path:

[james@raxus] scp -r james@backup.example.com:/home/james/Pictures/London\ Trip/First\ Day .

But, nope. You’ll get the same errors:

scp: /home/james/Pictures/London: No such file or directory
scp: Trip/First: No such file or directory
scp: Day: No such file or directory

How about putting double quotes around the parts of the file path with spaces?

[james@raxus] scp -r james@backup.example.com:/home/james/Pictures/"London Trip/First Day" .

Again, nope. You’ll get the same errors:

scp: /home/james/Pictures/London: No such file or directory
scp: Trip/First: No such file or directory
scp: Day: No such file or directory

There are several options that do work, but the two options I find the most human readable, and easiest to remember, are the following.

The first working option, and the one I prefer, is to put double quotes around the entire source target’s file path and escape the particular parts of the source target’s file path containing spaces:

[james@raxus] scp -r james@backup.example.com:"/home/james/Pictures/London\ Trip/First\ Day" .

The second working option is to put single quotes around the entire source target and put double quotes around the first part of the source target’s file path containing spaces to the very end of the source target’s file path:

[james@raxus] scp -r 'james@backup.example.com:/home/james/Pictures/"London Trip/First Day"' .

Some additional working options can be found at the link(s) in the References section.

References

How to escape spaces in path during scp copy in linux?



comments powered by Disqus