Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
Remote Sync (rsync) is a utility for efficiently transferring and synchronizing files and directories across networked computers.
In this tutorial, we’ll learn how to use it to copy files between two networked computers that both have rsync installed.
rsync provides many advantages as a file-copying tool, as it:
Now that we know about rsync and its advantages, let’s learn how to install it.
rsync comes pre-installed on most Linux distros. But, if it got deleted, we can re-install it:
# On Debian-based distros
$ sudo apt update
$ sudo apt install rsync
# On Fedora
$ sudo dnf install rsync
# On CentOS and other Red Hat-based distros
$ sudo yum install rsync
# On openSUSE
$ sudo zypper install rsync
# On Arch Linux
$ pacman -S rsync
After that, we can check if it’s installed:
$ rsync --version
rsync version 3.1.3 protocol version 31
Copyright (C) 1996-2018 by Andrew Tridgell, Wayne Davison, and others.
We’ve successfully installed rsync.
Generally, when calling rsync, we need to use this pattern:
$ rsync [options] [source files and directories] [destination]
Now let’s see more specific cases.
To copy files from the local system to the remote system, we can call rsync in this manner:
$ rsync -a /home/user/Downloads/file.txt [email protected]:/home/Files
[email protected]'s password:
In the above command:
After running the command, it’ll ask us to enter the SSH password. After that, rsync will copy the file.
To copy a directory from the local system to the remote system, we can:
$ rsync -a /home/user/Downloads/Temp [email protected]:/home/Files
[email protected]'s password:
The command will copy the Temp directory inside the /home/Files directory on the remote system. It’ll ask for the SSH password again, and then it’ll copy the directory.
To copy all files and directories inside a directory, we can run rsync like this:
$ rsync -a /home/user/Downloads/Temp/ [email protected]:/home/Files
After that, rsync will copy every file and folder inside the Temp directory to /home/Files.
Moreover, we can see that this time, the source directory has a trailing slash ‘/’ which means the content of Temp will be copied and not itself.
We can also use the –exclude and –exclude-from options to exclude file names that match specific patterns. –exclude receives a single pattern, while —exclude-from receives multiple patterns located inside a file.
For example:
$ ls
file1.txt file2.txt file3.txt new_file.pdf
$ rsync -a --exclude="*.txt" /home/user/Downloads/Temp/ [email protected]:/home/Files
[email protected]'s password:
This will copy exclude all the text files inside the Temp directory. Therefore, only new_file.pdf will be copied.
Let’s see another example:
$ ls
exclude_patterns.txt file1.txt file2.txt file3.txt new_file.pdf picture.jpg
$ cat exclude_patterns.txt
*.txt
*.pdf
$ rsync -a --exclude-from="exclude_patterns.txt" /home/user/Downloads/Temp/ [email protected]:/home/Files/Temp
[email protected]'s password:
This time, we had multiple patterns that we wanted to give rsync to exclude from. So, we wrote them inside a file named exclude_patterns.txt and gave it to rsync using the –exclude-from option.
After running the above command, only picture.png gets copied to the remote directory.
To transfer large amounts of data, we can add the -P option, which tells rsync to show progress during transfer and also to keep partially transferred files:
$ ls
file1.txt file2.txt file3.txt new_file.pdf picture.jpg video.mkv
$ rsync -a -P /home/user/Downloads/Temp/ [email protected]:/home/Files/Temp
[email protected]'s password:
sending incremental file list
./
file1.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=5/8)
file2.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=4/8)
file3.txt
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=3/8)
new_file.pdf
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=2/8)
picture.jpg
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/8)
video.mkv
245,379,066 100% 570.99kB/s 0:06:59 (xfr#7, to-chk=0/8)
After running the above command, rsync copies every file inside the Temp directory to the remote directory.
In this tutorial, we looked at several examples of using rsync in different scenarios. rsync provides many advantages when synchronizing files and directories across networked computers.