Main Tutorials

Copy file to / from server via SCP command

scp data transfer

SCP uses Secure Shell (SSH) to transfer data between client and remote server, it’s fast and secure. In this article, we will show you two common SCP copying examples :

  1. Copying data from your computer to remote server.
  2. Copying data from remote server to your computer.

1. Copying data to Remote Server

Example 1.1 – Copying a file.


scp SourceFile user@server:directory/TargetFile

Copy a file “gae.log” from local computer to remote server(mkyong.com), ‘/home/mkyong/‘ folder, via username “mkyong“.


$ scp gae.log [email protected]:/home/mkyong/

Password: 
stdin: is not a tty
gae.log     100% 4193     4.1KB/s   00:00    

Example 1.2 – Copying a folder, and all its content.


scp -r SourceFolder user@server:directory/TargetFolder

Copy a folder “tutorials” from local computer to server(mkyong.com), ‘/home/mkyong/‘ folder, via username “mkyong“.


$ scp -r tutorials [email protected]:/home/mkyong/

Password: 
stdin: is not a tty
aweber.txt       100% 2895     2.8KB/s   00:01    
codetraining.txt 100%  330     0.3KB/s   00:00    
contact.txt      100%  146     0.1KB/s   00:00    
eclipse.txt      100% 5719     5.6KB/s   00:00    

2. Copying data from Remote Server

Example 2.1 – Copying a file.


scp user@server:directory/SourceFile TargetFile

Copy a file “/home/mkyong/modsec_audit.log” from remote server(mkyong.com), to local, current folder (“.“), via username “root“.


$ scp [email protected]:/home/mkyong/modsec_audit.log .

Password: 
stdin: is not a tty
modsec_audit.log  100%  244KB 122.1KB/s   00:02    

Example 2.2 – Copying a folder, and all its content.


scp -r user@server:directory/SourceFolder TargetFolder

Copy a folder “/home/mkyong/mail” from remote server(mkyong.com), to local, current folder (“.“), via username “mkyong“.


Yongs-MacBook-Air:~ mkyong$ scp -r [email protected]:/home/mkyong/mail .
Password: 
stdin: is not a tty
courierimapuiddb      100%   15     0.0KB/s   00:00    
courierpop3dsizelist  100%   88     0.1KB/s   00:01    
courierimapacl        100%   43     0.0KB/s   00:00    
maildirsize           100%  348     0.3KB/s   00:00    
SSH Port
If your server is using non-default SSH port 22, uses -P portnumber option (Uppercase P) :


scp -P 2222 user@host:directory/SourceFile TargetFile

References

  1. Secure Copy (SCP) in Wiki
  2. SSH and SCP how to tips and tricks

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Marcelo
1 year ago

Do you know any Java clients to do this?

Java Fan
5 years ago

Can you post how to do this in Java

Bhaskar Narayana Setty
7 years ago

Hi ,
I want to copy a file from my local windows machine to a Linux server . I executed the command ” scp SourceFile user@server:directory/TargetFile ” in my linux. But I am not able to copy a file and getting an error as
ssh: C: node name or service name not known.

can you please guide me

Thanks,
Bhaskar

Daniel Didusch
9 years ago

Thank you very much for your great tutorial! It helped me a lot.

tomasio reicht
9 years ago

Finally some tut enabling me to copy my pics via scp from my phone to my computers. thx a lot 😉