Copying files from one Linux host to another
As I don’t copy files from one linux host to another often enough to know the commands with all the switches by heart, I decided to create a blog post about it.
So say you need to copy a single file from host A to host B. Good old scp (secure copy) would probably be the easiest way to achieve this:
scp source_file_name username@destination_host:destination_folder
Obviously the username in above command needs to have write access to the destination folder.
If you need to copy an entire directory including possible subdirectories from host A to host B, then you have the -r switch. The -p switch is also handy to preserve file attributes:
scp -pr /data/* root@host:/data
or from host B:
scp -pr root@host:/data/* /data
However, with a lot of little files scp starts to become quite inefficient and thus slow. It will be faster to throw the files in an archive, copy the archive and then unpack them at the destination. You can do this all in a single command:
tar -cf - source_dir | ssh user@hostB "cd target_dir; tar -xf -"
Another nice command to copy files is rsync. Advantage of rsync is that it can keep directories in sync, so if you need to copy the delta between two directories, rsync is very efficient.
rsync -aE -e ssh directory user@hostB:target_dir
or from hostB:
rsync -aE -e ssh user@hostA:directory target_dir
If network speed is an issue, add the -z switch to compress the data. The -r switch copies recursively
useful links:
Thank you