Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 254
AlexPandiyan Avatar asked Nov 08 '13 11:11

AlexPandiyan


People also ask

How do I copy an entire directory using scp Linux?

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents. You'll be prompted for your password on the source system ( deathstar.com ). The command won't work unless you enter the correct password.

How do I use scp without overwriting existing files?

More specifically, what you can do is to make all destination files "read-only" before scp transfer. This will prevent any existing destination files from being overwritten by scp . After scp transfer is completed, restore the file permissions to the original state.


2 Answers

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 [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 . 
like image 157
Adrian Gunawan Avatar answered Sep 29 '22 03:09

Adrian Gunawan


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.

like image 35
Vorsprung Avatar answered Sep 29 '22 04:09

Vorsprung