Transferring and Synchronizing Files with rsync
Learn how to transfer and synchronize files between two machines using the rsync command.
We'll cover the following
rsync
The rsync
program can copy files and folders between two locations on the same system and transfer files between local and remote locations.
Let’s use rsync
to transfer the testsite
directory from one user to another. This time, we copy it to a new folder named testsite2
.
We execute this command to perform the copy:
rsync -v -r testsite/ {username}@{ip}:testsite2
Notice the trailing slash for the source. This tells rsync
to copy the directory contents to the destination rather than the directory itself. Unlike the scp
command, we can use this for the first transfer and subsequent transfers. The destination directory is created if it doesn’t exist.
Let’s try it out. We run the command again and then use tree
to view the files on the remote machine:
rsync -v -r testsite/ {username}@{ip}:testsite2
ssh {username}@{ip} 'tree testsite2'
Follow these steps:
- Upon the first prompt, enter
123456
as the password. - Type
su sshuser
. - Go to the
sshuser
folder by typingcd /home/sshuser
- Type
rsync -v -r testsite/ temp@127.0.0.1:testsite2
. - Upon the next prompt, type
yes
and press “Enter”. - Enter the password you set earlier and press “Enter”.
- To verify that the files have been copied, type
ssh temp@127.0.0.1 'tree testsite2'
, and enter the password when prompted.
Get hands-on with 1400+ tech skills courses.