I am connected with ssh and I want to copy a directory from my local to our remote server; how can I do that?
I have read several post using scp but that doesn't worked for me. Some posts suggested using rsync but in my case I just want to copy one directory.
- 103
- 4
- 423
3 Answers
If you want to copy a directory from machine a to b while logged into a:
scp -r /path/to/directory user@machine_b_ipaddress:/path/to/destination
If you want to copy a directory from machine a to b while logged into b:
scp -r user@machine_a_ipaddress:/path/to/directory /path/to/destination
- 103
- 1,082
-
I think more intuitive answer would be "from machine b to a while logged into a" anybody can swap a in the head, but would indicate source/destination much better, unless I'm crazy :) – Pawel Cioch Apr 10 '20 at 20:26
-
@PawelCioch While I definitely agree with you, it's not difficult to comprehend. I did write this answer almost 3 years ago, and I wasn't very good with English :) – Erik Apr 11 '20 at 00:17
-
3Well several years on, 2020, and I thank you for your answer, found it easy to read with no problems. Worked like a charm. – redfox05 Dec 01 '20 at 12:01
-
Well, now the question is "how to be logged into b". – Nairum Mar 31 '23 at 12:59
You can use cpio or tar to create an archive as a stream on standard output, pipe that to ssh and extract the stream on the remote host. For example, using tar:
tar cf - dir | ssh remotehost 'tar xf -'
To extract the archive in a different directory on the remote host, use
tar cf - dir | ssh remotehost 'tar xfC - /path/on/remote'
If your tar supports the C option or:
tar cf - dir | ssh remotehost '
cd /path/on/remote && tar xf -'
if not.
If on a low bandwidth connection, you may want to compress the stream:
tar cf - dir | gzip -3 | ssh remotehost '
cd /path/on/remote && gunzip | tar xf -'
(replace gzip/gunzip with your stream compressor of choice, lzop/lzop -d may be a better choice if you find that CPU is the bottleneck).
- 544,893
- 13,168
Think this might work for you:
scp file user@host:/location_to_save_file
scp - secure copy
- the file(s) you want to scp to remote node
- the user who has permissions to scp file, i.e sysadmin, etc
@ - user and host separator
host - the node you are scp the file(s)
:/location_to_save_file - absolute path to save the file
- 1,082
- 277
-
With the addition that you need to add the
-rflag if you want to copy a directory instead of a file, like @hrk is asking. – Axel Köhler Dec 22 '22 at 08:25