HowTo: Copy Files Over The Network using the Ubuntu Command Line
As I continued to struggle in this GUI-less word, the need for transferring files over the network arose. I needed to get a lot of files from one place to another and I just simply didn’t know how to do it. Linux obviously offers a number of alternatives for transferring / copying files over the network and you can also configured shared folders over the network. For those who are new to Linux/Ubuntu/Debian/etc. here are a few ways you can accomplish a file transfer over ssh:
I tend to use Secure Copy most of the time as it was the first tool I was introduced to. The syntax is rather simple and its use is very straighforward. If you want you can find the manual of SCP with its syntax here: SCP.
To use SCP simply do the following:
type scp followed by the -r directive to indicate recursive (sub folders, etc.). After that you need to type the server and the username used to connect to it in the following format [email protected] Immediately after that you will put a colon and follow it by the destination route.
For example, if you wanted to copy all your conf folders (think that you have three folders with the names of: conf.sites, conf.common, and conf.ssl)
scp -r /etc/nginx/conf.* [email protected]:/etc/nginx
That command will copy all the folders/files in the current directory that match the pattern conf.* to /etc/nginx in the target server. The result in this case would be three folders with all their contents copied over.
RSync is a new tool I found out about which has a few added benefits and it is maintained by the guys at Samba. You can find a copy of the manual here for your perusal: RSync. Rsync has the advantage that out of the box it will copy not only the file but the file permissions among other things (ownership, time stamps, etc.) This is done by using the -a parameter which indicates archive.
Using Rsync is not much different than using SCP. Simply follow this basic example:
Suppose you want to copy the directory ~/myDirectory from user UserName on machine ServerName to your local machine and in the current directory. For this you would run the following command:
rsync -avzn [email protected]:~/myDirectory .
Note that we used the -n parameter which will make rsync to only output the actions it will perform without actually doing it. This is good for testing and making sure you’ll get the desired outcome. If you hare happy with it, simply remove the -n option and perform the file copy operation via ssh:
rsync -avz [email protected]:~/myDirectory .
NOTE: For either method to work you need SSH to be working on both the client and the server machines.