While the previous answers are technically correct, you should also consider using rsync instead. rsync compares the data on the sending and receiving sides with a diff mechanism so it doesn't have to resend data that was already previously sent.
If you are going to copy something to a remote machine more than once, use rsync. Actually, it's good to use rsync every time because it has more controls for things like copying file permissions and ownership and excluding certain files or directories. In general:
$ rsync -av /local/dir/ server:/remote/dir/
will synchronize a local directory with a remote directory. If you run it a second time and the contents of the local directory haven't changed, no data will be transferred - much more efficient than running scp and copying everything every time.
Also, rsync allows you to recover from interrupted transfers very easily, unlike scp.
Finally, modern versions of rsync by default run over ssh, so if scp is already working, rsync should pretty much be a drop-in replacement.
You can also extract files on target system by using pipes. This command will copy contents of /var/log at domain.com to ~/destination/log on your system:
ssh -p 22 [email protected] 'cd /var && tar zcvf - log' | tar xzf - -C ~/destination
Though to mirror a directory, you probably should use rsync...
If you prefer to pass the user's password as a parameter rather than inputting it interactively, you can use sshpass (sudo apt-get install -y sshpass).
You can use -r option with scp command to copy directories recursively on any system. If you need anything else refer scp command tutorial. -r option stands for recursive operation in most of the Linux commands.
Yup, use
-r
:Note: This creates the
sourcedirectory
inside/path
thus the files will be in/path/sourcedirectory
While the previous answers are technically correct, you should also consider using
rsync
instead.rsync
compares the data on the sending and receiving sides with a diff mechanism so it doesn't have to resend data that was already previously sent.If you are going to copy something to a remote machine more than once, use
rsync
. Actually, it's good to usersync
every time because it has more controls for things like copying file permissions and ownership and excluding certain files or directories. In general:will synchronize a local directory with a remote directory. If you run it a second time and the contents of the local directory haven't changed, no data will be transferred - much more efficient than running
scp
and copying everything every time.Also,
rsync
allows you to recover from interrupted transfers very easily, unlikescp
.Finally, modern versions of
rsync
by default run over ssh, so ifscp
is already working,rsync
should pretty much be a drop-in replacement.That is what the
-r
option is for. :)See the scp man page for more info if needed.
Recursive Copy Option '-r' (lower case)
Which I confuse with the regular local recursive copy option '-R' (upper case)
The best way is to use rsync over SSH
My favorites options are -Pazvessh --delete :
After looking for the recursive copy flag, and successfully used it thanks to this post, I would like to post just a suggestion.
If the case is that you are copying (recursively) a directory. Maybe if the files are sent compressed you could save time in the transfer
What I did in the end was:
Hope this helps
You can recursively copy a directory into a compressed archive with this simple command:
For example, to copy contents of
/var/log
fromdomain.com
to~/logs.tgz
you run:You can also extract files on target system by using pipes. This command will copy contents of
/var/log
atdomain.com
to~/destination/log
on your system:Though to mirror a directory, you probably should use
rsync
...If you prefer to pass the user's password as a parameter rather than inputting it interactively, you can use
sshpass
(sudo apt-get install -y sshpass
).Example:
You can use -r option with scp command to copy directories recursively on any system. If you need anything else refer scp command tutorial. -r option stands for recursive operation in most of the Linux commands.