I am trying to copy files using secure copy(scp). I am trying to execute the following command but I get error due to the space in the absolute path of the location of the directory.
scp -r -P 8484 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="01736e6e75413036332f32302f36332f303132">[email protected]</a>:/media/New Volume/lj /home/pratheep
I am getting the following error:
scp: /media/New: No such file or directory scp: Volume/lj: No such file or directory
I tried using the same command putting extra back slash like
scp -r -P 8484 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="285a47475c68191f1a061b19061f1a0619181b">[email protected]</a>:/media/New Volume/lj /home/pratheep
but I am still getting the same error.
Can somebody tell me how to correct it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You should quote your file name two times, one for the local shell and one for the remote one. In the simplest case you can do one of the following
scp -r -P 8484 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16647979625627212438252738212438272625">[email protected]</a>:"'/media/New Volume/lj'" /home/pratheep scp -r -P 8484 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e4968b8b90a4d5d3d6cad7d5cad3d6cad5d4d7">[email protected]</a>:'"/media/New Volume/lj"' /home/pratheep
or using the help of tab completion
scp -r -P 8484 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d1f0202192d5c5a5f435e5c435a5f435c5d5e">[email protected]</a>:/media/New\ Volume/lj /home/pratheep
Method 2
scp and rsync pass remote file names to the remote shell. This allows you to pass patterns, as in
scp remotehost:'*.txt' .
but it has the nasty consequence that you need extra quoting when supplying a remote file name.
You can pass the -s option to rsync to make it protect the file names from expansion by the remote shell. But that turns on rsync’s built-in wildcard expansion, so you still need to quote [?*.
A workaround is to transfer an archive:
ssh -P 8484 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddafb2b2a99deceaeff3eeecf3eaeff3ecedee">[email protected]</a> 'cd "/media/New Volume" && tar cf - lj' | tar xf - -C /home/pratheep
Another method is to mount the remote filesystem, and then use ordinary tools to perform the copy.
mkdir mnt sshfs -p 8484 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6a4b9b9a296e7e1e4f8e5e7f8e1e4f8e7e6e5">[email protected]</a>:/ mnt cp -Rp 'mnt/media/New Volume/lj' /home/pratheep fusermount -u mnt rmdir mnt
Method 3
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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8a8c9a8dbf97908c8b">[email protected]</a>:'dir with spaces/file with spaces' <destination> scp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dca9afb9ae9cb4b3afa8">[email protected]</a>:"dir\ with\ spaces/file\ with\ spaces" <destination> scp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbaea8bea99bb3b4a8af">[email protected]</a>:dir\ with\ spaces/file\ with\ spaces <destination>
Note: does not require option -T (see below).
Use double-quoting + option -T:
scp -T <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="116462746351797e6265">[email protected]</a>:"'<path-with-spaces>'" <destination> scp -T <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097c7a6c7b4961667a7d">[email protected]</a>:'"<path-with-spaces>"' <destination> scp -T <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9feaecfaeddff7f0eceb">[email protected]</a>:""<path-with-spaces>"" <destination>
Note: without option -T, these commands will result in 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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daafa9bfa89ab2b5a9ae">[email protected]</a>:"${source}" <destination>
Note: this works fine without option -T, but only for a single file. For multiple files, option -T is required again (same error as above).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0