On a computer with IP address like 10.11.12.123
, I have a folder document
. I want to copy that folder to my local folder /home/my-pc/doc/
using the shell.
I tried like this:
scp -r smb:10.11.12.123/other-pc/document /home/my-pc/doc/
but it's not working.
If you do include a trailing / after document (i.e. 10.11. 12.123:/other-pc/document/ ) you are telling rsync to copy the contents, (i.e. the files and directories under) document to 10.11. 12.123:/other-pc/ without also copying the document directory.
To copy files or directories in Unix-based operating systems (Linux and MacOS), you use the cp command. The cp command is a relatively simple command, but its behavior changes slightly depending on the inputs (files vs directories) and the options you pass to it.
In order to copy directory on Linux to remote location, you can execute the “scp” command with the “-r” option for recursive followed by the directory to be copied and the destination folder. As an example, let's say that we want to copy the “/etc” directory to a backup server located at 192.168.
Copy or Download a File From Remote to Local Using SCP SCP syntax is pretty simple. Just invoke SCP followed by the remote username, @, the IP address or host, colon, and the path to the file. If not specified, the default path is the remote user's home directory.
So you can use below command to copy your files.
scp -r <source> <destination>
(-r
: Recursively copy entire directories)
eg:
scp -r [email protected]:/other-pc/document /home/my-pc/doc
To identify the location you can use the pwd
command, eg:
kasun@kasunr:~/Downloads$ pwd
/home/kasun/Downloads
If you want to copy from B to A if you are logged into B: then
scp /source username@a:/destination
If you want to copy from B to A if you are logged into A: then
scp username@b:/source /destination
In addition to the comment, when you look at your host-to-host copy options on Linux today, rsync
is by far, the most robust solution around. It is brought to you by the SAMBA team[1] and continues to enjoy active development. Most distributions include the rsync
package by default. (if not, you should find an easily installable package for your distro or you can download it from rsync.samba.org ).
The basic use of rsync
for host-to-host directory copy is:
$ rsync -uav srchost:/path/to/dir /path/to/dest
-uav
simply recursively copies -ua
only new or changed files preserving file & directory times and permissions while providing -v
verbose output. You will be prompted for the username/password on 10.11.12.123
unless your have setup ssh-keys
to allow public/private key authentication (see: ssh-keygen
for key generation)
If you notice, the syntax is basically the same as that for scp
with a slight difference in the options: (e.g. scp -rv srchost:/path/to/dir /path/to/dest
). rsync
will use ssh
for secure transport by default, so you will want to insure sshd
is running on your srchost (10.11.12.123
in your case). If you have name resolution working (or a simple entry in /etc/hosts
for 10.11.12.123
) you can use the hostname for the remote host instead of the remote IP. Regardless, you can always transfer the files you are interested in with:
$ rsync -uav 10.11.12.123:/other-pc/document /home/my-pc/doc/
Note: do NOT include a trailing /
after document
if you want to copy the directory itself. If you do include a trailing /
after document
(i.e. 10.11.12.123:/other-pc/document/
) you are telling rsync
to copy the contents, (i.e. the files and directories under) document
to 10.11.12.123:/other-pc/
without also copying the document
directory.
The reason rsync
is far superior to other copy apps is it provides options to truly synchronize filesystems and directory trees both locally and between your local machine and remote host. Meaning, in your case, if you have used rsync
to transfer files to /home/my-pc/doc/
and then make changes to the files or delete files on 10.11.12.123
, you can simply call rsync
again and have the changes/deletions reflected in /home/my-pc/doc/
. (look at the several flavors of the --delete
option for details in rsync --help
or in man 1 rsync
)
For these, and many more reasons, it is well worth the time to familiarize yourself with rsync
. It is an invaluable tool in any Linux user's hip pocket. Hopefully this will solve your problem and get you started.
Footnotes
[1] the same folks that "Opened Windows to a Wider World" allowing seemless connection between windows/Linux hosts via the native windows server message block (smb) protocol. samba.org
If the two directories (document
and /home/my-pc/doc/
) you mentioned are on the same 10.11.12.123
machine.
then:
cp -ai document /home/my-pc/doc/
else:
scp -r document/ [email protected]:/home/my-pc/doc/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With