This answer is almost 12 years old, however I am using ssh transfer a lot and I am going to share some experiences.
It is possible to use scp for transfer whole directory, however it has disadvantages compare to rsync. I was using scp for short time before i replaced scp with rsync. Package rsync should be almost at every Linux computer or server.
rsync work for synchronize whole directory with sub-directories. It can be used for synchronize two directories at different places on one computer-server or synchronize two directories, one directory is local and second directory at remote host. Source or destination can be remote or local (local = at server or computer, where you have accounts).
Command Structure is:
rsync -ahv -e ssh --delete --progress --bwlimit=2048k RemoteUser@RemoteHost:/Address_Of_Source_Directory/ /Address_Of_Destination_Directory
Address (full path) of source or target directory begin always with /
RemoteUser@RemoteHost: before path is for remote source-destination. This is not used for local source-destination.
Now lets look to commands:
rsync -ahv -e ssh --delete --progress --bwlimit=2048k Source_Full_Path Target_Full_Path
-e ssh it means that you use ssh protocol for transfering from-to remote host.
--progress showing, how rsync command is running (file by file)
--delete files and directories, that are at destination, but not at source, will be deleted.
--bwlimit=2048k it is limit of transfer speed in kB, to avoid traffic jam of internet network. Value can be changed and this do not need to be in command.
To make sure, that content of source and destination is the same, you can run rsync firs with -ahv (it test files and directories by date of change) and second time with -ahvc, it makes checksum for every file at source and destination (probably sha256sum), and you are sure that content of all files is the same. It can take longer time with plenty of files or many many gigabites of data.
rsync works good for lot of files, however you can compress whole directory with directory structure. Tar is not good if names of files are not only alphanumeric. Better is 7z (p7zip). Go to directory with data and then start 7zip. Best compression results are probably:
7z a -t7z -m0=lzma2 -mqs -mx=9 -mfb=273 -md=64m -ms=on -mmt=2 Name_of_archive.7z
-mqs it means that files in row are sorted by type (txt jpg pdf etc), not by directory structure and names. It can get better results.
-md=64m Something like size of blocks, dictationary. 64m is enough (64MB). You can add more, but it takes at least 11-12 times more RAM than size of blocks.
-mmt=2 numer of CPU treads. Use -mmt=off for one tread (if you have lot of small files) or -mmt=2. If you use more that 2 treads, it will takes more RAM. 7z takes ram something like this (numer of pairs of CPU treads * block-dictatonary size)*(around 11-12 times) or if you have lot of files (milions), it takes few more GiB of RAM.
ssh -C
orCompression yes
in~/.ssh/config
). – sam hocevar Mar 25 '11 at 01:06$ tar cz <files> | ssh user@host "cd /wherever; tar xvz"
– carlodef Jul 28 '15 at 18:14-C
option as described in @forcefsk's answer is safer. Alternatively use&&
instead of;
– Bram Jul 21 '16 at 10:55f
defaults to stdin or stdout, but (most? all?) other tars do not. – dave_thompson_085 Feb 16 '18 at 05:42-I
option is not portable)) (On say a 1 Gbps connection, gzip might end up being the bottleneck - but without huge amounts of data, say a TB or two, it is probably not worth it) – Gert van den Berg Dec 20 '19 at 07:45ssh user@host "tar cz files" | tar xvz
– lionello Dec 07 '21 at 05:28cd
in the way that is done in the answer currently is safe, as it preventstar
from running if thecd
fails. Using-C
withtar
would be prettier though. – Kusalananda Feb 26 '22 at 11:12&&
instead of;
) it is indeed safe. That was one of the improvements I suggested. At the time of writing my comment that bit wasn't in the answer. – Bram Feb 28 '22 at 15:25find ./path/to/dir -name "*.gz" | xargs gunzip
– John Mellor Jul 20 '22 at 11:33