Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy a folder from remote to local using scp?

How do I copy a folder from remote to local host using scp?

I use ssh to log in to my server.
Then, I would like to copy the remote folder foo to local /home/user/Desktop.

How do I achieve this?

like image 826
Slasengger Avatar asked Jul 03 '12 05:07

Slasengger


People also ask

How do I SCP from remote to local machine?

To copy the files you will need to first invoke the SCP, followed by the remote username@IP address, path to file. If you do not specify the path, it is assumed as default in this case which will be the user's home directory, this will be followed the path where the file will be stored locally.

Can you SCP from remote to local?

Copy or Download a File From Remote to Local Using SCP SCP syntax is pretty simple. Just invoke SCP followed by the remote username, @, the IP address or host, colon, and the path to the file. If not specified, the default path is the remote user's home directory.

Can I SCP a folder?

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

Can you transfer a file from the remote machine to the local machine?

Copy and paste the required file from the remote machine in the cloud storage disk. Now, the file will appear in the file transfer window as shown below. Click the download icon against the file. Now the file will be downloaded to your local machine.


2 Answers

scp -r [email protected]:/path/to/foo /home/user/Desktop/ 

By not including the trailing '/' at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory.

From man scp (See online manual)

-r Recursively copy entire directories

like image 57
Gryphius Avatar answered Sep 21 '22 17:09

Gryphius


To use full power of scp you need to go through next steps:

  1. Public key authorisation
  2. Create ssh aliases

Then, for example if you have this ~/.ssh/config:

Host test     User testuser     HostName test-site.com     Port 22022  Host prod     User produser     HostName production-site.com     Port 22022 

you'll save yourself from password entry and simplify scp syntax like this:

scp -r prod:/path/foo /home/user/Desktop   # copy to local scp -r prod:/path/foo test:/tmp            # copy from remote prod to remote test 

More over, you will be able to use remote path-completion:

scp test:/var/log/  # press tab twice Display all 151 possibilities? (y or n) 

Update:

For enabling remote bash-completion you need to have bash-shell on both <source> and <target> hosts, and properly working bash-completion. For more information see related questions:

How to enable autocompletion for remote paths when using scp?
SCP filename tab completion

like image 44
Alexander Yancharuk Avatar answered Sep 19 '22 17:09

Alexander Yancharuk