sending files between linux computers

Sending files between Linux computers

In order to send files between computers, the easiest and safest way I have found so far is using rsync. It transfers files securely over SSH. In this setup, assume that you are on the sender computer and that the remote machine is the receiver.

make sure ssh works first

Before using rsync, make sure the sender can SSH into the receiver. If SSH access is not set up yet, follow Allowing SSH access between Linux computers first.

ssh cjm@1.2.3.4

Replace cjm with the username on the receiver, and replace 1.2.3.4 with the receiver's IP address or hostname.

install rsync

rsync should be installed on both the sender and the receiver.

sudo apt update
sudo apt install rsync

send files to the receiver

Copy the contents of a local folder on the sender to a folder on the receiver:

rsync -avz ./local-folder/ cjm@1.2.3.4:/home/cjm/remote-folder/

A common real shape is:

rsync -avz ./site/ cjm@1.2.3.4:/home/cjm/site/

copy files back from the receiver

Reverse the source and destination to pull files from the receiver back to the sender:

rsync -avz cjm@1.2.3.4:/home/cjm/remote-folder/ ./local-folder/

trailing slash

The trailing slash changes what gets copied.

rsync -avz folder/ cjm@1.2.3.4:/home/cjm/dest/

This copies the contents of folder into dest.

rsync -avz folder cjm@1.2.3.4:/home/cjm/dest/

This copies the folder directory itself into dest.

useful options

# archive mode, verbose, compressed
rsync -avz source/ dest/

# show what would happen without copying
rsync -avzn source/ dest/

# delete destination files that no longer exist in the source
rsync -avz --delete source/ dest/

Be careful with --delete. It is useful for mirroring directories, but it can remove files on the receiver.

custom ssh port

If the receiver's SSH server uses a non-default port, pass an SSH command with -e:

rsync -avz -e "ssh -p 2222" ./local-folder/ cjm@1.2.3.4:/home/cjm/remote-folder/

If ssh cjm@1.2.3.4 works from the sender, then rsync should work too.


edit this page