How to copy a file/directory between servers on Linux

So, you have a file or entire directory to copy from one server to another. The first thing you considered was probably download it to your local machine and upload it to new server. Then you asked yourself – why? And you ended up here :)

Easiest way to copy is directly from one server to another.

If you have a directory best thing you can do is to compress it first. This little line of code will create a tar/gzip file with entire directory content compressed inside – and you have a compressed file:

tar -zcvf filename.tgz /path/to/directory

-z: Compress archive using gzip program
-c: Create archive
-v: Verbose i.e display progress while creating archive
-f: Archive File name

All you need to do now is to copy it to another server:

scp filename.tgz username@remotehost.com:/path/to/target/directory

If you want to copy entire directory (not one file) use ‘-r’ switch (recursive):

scp -r /path/to/source/directory username@remotehost.com:/path/to/target/directory

But it’s much better and faster to copy a compressed file. 
Once compressed file is copied you’ll need to uncompress it:

tar -zxvf filename.tgz

-x: Extract files

And that’s all folks.

This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply