Copying Files and Directories with rsync
- WHAT?
You can use the rsync tool to copy files locally or remotely. rsync is unidirectional and can copy files only in one direction.
- WHY?
This article provides an overview of the rsync tool and its basic usage for copying files locally and remotely.
- EFFORT
It takes up to 20 minutes to understand the basic usage of the rsync tool.
- GOAL
Gain a foundational understanding of rsync and how to use it for file transfers.
- REQUIREMENTS
Access permissions to the source and destination locations for copying files.
1 The rsync concept #
You can use the rsync tool for transferring or copying files locally and remotely. rsync is unidirectional. It copies files in a single direction and does not synchronize data.
Instead of transmitting all the contents in the file, rsync minimizes the amount of data copied by moving only the changes within the file. Hence, this supports the transfer of data over a slow network connection.
For copying large files using rsync, detecting changes requires certain computing power. So ensure that machines on both source and destination have enough resources, including RAM.
Use rsync when a large amount of data containing only minor changes needs to be transmitted regularly. This is useful when working with backups. rsync can also be useful for mirroring staging servers that store complete directory trees of Web servers to a Web server in a DMZ.
This article includes examples that show the usage of rsync to copy files locally and remotely. This document also explains how to configure and use an rsync server.
2 Basic rsync usage #
You can use rsync on any machine where you have access and write permissions.
Copy files from the source location to the destination location as shown in the following example:
>rsync [OPTION] SOURCE [SOURCE]... DESTINATION
You can have multiple SOURCE entries and the SOURCE and DESTINATION placeholders can be paths, URLs or both.
Listed below are a few use cases of copying files:
>rsync -av /directory1/ /destination/In the above example, the trailing slash after the directory denotes the content of the directory and not the directory itself. In this, only the contents of
/directory1are copied to/destination.>rsync -av /directory1 /destination/In the above example, the directory
/directory1is copied to/destinationresulting in/destination/directory1/.>rsync -av example1.txt example2.txt /destination/directory/The above example shows copying the files
example1.txtandexample2.txtto the destination directory.
The commonly used OPTIONS are listed below:
-Vshows verbose output text
-aarchive mode; copies files recursively and preserves time stamps, user/group ownership, file permissions and symbolic links
-zcompresses the transmitted data
-nor--dry-runperforms a trial run with no changes made
-rrecurse into directories
--progressshows progress during transfer
-btakes a backup during data synchronization
-hshows the numbers in the output in a human-readable format
-efor remote transfers, instructs rsync to use the SSH protocol
-q or --quietsilences the rsync command and option output
--versionprints the rsync version number and exit
- Copy files locally with compression
>rsync -avz example1.txt example2.txt /destination/-aarchive mode-vverbose-zcompress data during transfer- Copy a directory with compression
>rsync -avz /sourcedirectory/ /destinationdirectory/Transfers
/sourcedirectory/contents with compression.- Remote file transfer with compression
>rsync -avz example1.txt user@remote_host:/remote/destination/Transfers
example1.txtto a remote machine over SSH while compressing the data.- Remote directory transfer with compression
>rsync -avz /sourcedirectory/ user@remote_host:/remote/destination/Transfers
/sourcedirectory/to a remote machine over SSH while compressing the data.
3 Copying files and directories with rsync locally #
You can copy files within the same machine using rsync if you have write permission for the destination location.
To copy the file backup.tar.xz to the location /var/backup/, use the command
rsync -avz backup.tar.xz /var/backup/.
The file backup.tar.xz is copied to /var/backup/, and the absolute path is/var/backup/backup.tar.xz.
4 Copying files and directories with rsync remotely #
You can copy files from one machine to a remote machine using rsync. Ensure that you have rsync in both source and destination machines and that you are connected to the remote machine over SSH.
To copy a single file from one directory on your machine to the /var/backup directory on a remote machine, use the following command:
>rsync -avz example1.txt user@remote_host:/var/backup/
Ensure that you add the trailing slash after the/var/backup/ directory. If you do not insert the slash, the file example1.txt is copied to /var/backup file and not inside the directory /var/backup/.
Verify the copied file in the absolute path /var/backup/example1.txt.
To copy the directory source/ and its contents into the directory /var/backup/:
>rsync -avz /source/ user@remote_host:/var/backup/
Verify the copied directory in the absolute path /var/backup/source/.
5 Configuring and using an rsync server #
An rsync server enables file synchronization, automated backups and centralized storage. You can run rsync as a daemon rsyncd listening on default port 873 for incoming connections. This daemon can receive copying targets.
The configuration file for the rsync daemon is separated into a main file and certain modules which hold your backup target. This makes it easier to add additional targets later. You can store global values in /etc/rsyncd.d/*.inc files, whereas your modules are placed in /etc/rsyncd.d/*.conf.
The following description explains how to create an rsync server with a backup target. This target can be used to store your backups. To create an rsync server, do the following:
On your local machine, create a directory
/var/backupto store all your backup files.>mkdir /var/backupSpecify ownership:
In this example, the user
tuxin the groupusersowns the directory:>chown tux.users /var/backupConfigure the
rsyncddaemon:Create a directory
/etc/rsyncd.d/>mkdir /etc/rsyncd.d/Create the main configuration file
/etc/rsyncd.confand add the following lines:log file = /var/log/rsync.log1 pid file = /var/lock/rsync.lock2 merge /etc/rsyncd.d3 include /etc/rsyncd.d4
/var/log/rsync.logis the location where rsync writes the logs./var/lock/rsync.lockis the file that contains the PID of the running rsync daemon instance.Merges global values from
/etc/rsyncd.d/*.incfiles into the main configuration file.Loads any modules (or targets) from
/etc/rsyncd.d/*.conffiles. These files must not contain any references to global values.
Create your module or your backup target in the file
/etc/rsyncd.d/backup.confwith the following lines:backup.conf: backup module [backup] 1 uid = tux 2 gid = users 3 path = /var/backup 4 auth users = tux 5 secrets file = /etc/rsyncd.secrets 6 comment = Our backup target 7
In the given example:
[backup]is the backup target. You can use any name for the backup target. You may use a name according to its purpose and use the same name in your*.conffile.uid = tuxspecifies the user name or group name that is used when the file transfer takes place.gid = usersdefines the path to store your backups (from Step 1).path = /var/backupdefines the path to store your backups (from Step 1).auth users = tuxspecifies a comma-separated list of allowed users. In its simplest form, it contains the user names that are allowed to connect to this module. In our case, only user tux is allowed.secrets file = /etc/rsyncd.secretsspecifies the path of a file that contains lines with user names and plain passwords.Create the
/etc/rsyncd.secretsfile with the following content and replacePASSPHRASE:# user:passwd tux:PASSPHRASE
Make sure the file is only readable by root:
chmod 0600 /etc/rsyncd.secrets
Start and enable the
rsyncddaemon with:>systemctl enable rsyncd>systemctl start rsyncdOn the remote machine, test the access to your
rsyncserver:>rsync jupiter::A response similar to the following appears:
backup Our backup target
If the above response does not appear, verify your configuration file, firewall and network settings.
The above steps create an
rsyncserver that you can use to store backups. The example also creates a log file listing all connections. This file is stored in/var/log/rsyncd.log. This is useful for debugging your transfers.To list the content of your backup target, use the following command on your local machine:
>rsync -avz jupiter::backupThis command lists all files present in the directory
/var/backupon the server. This request is also logged in the log file/var/log/rsyncd.log.To copy the current directory to your
rsyncbackup server, provide the source directory. If the source directory is your current directory, use.>rsync -avz . jupiter::backupBy default,
rsyncdoes not delete files and directories when it runs. To enable deletion, the additional option--deletemust be stated. To ensure that no newer files are deleted, the option--updatecan be used instead. Any conflicts that arise must be resolved manually.
6 Deleting and updating files and directories with rsync #
You can copy and synchronize files within the same machine using rsync if you have write permission for the destination location. You can also delete files or directories after copying.
To copy files example1.txt and example2.txt from /source/directory/ to /destination/directory/:
>rsync -av --update /source/directory/ /destination/directory/
The rsync command updates the files example1.txt and example2.txt in the /destination/directory/ if it is older than the source file. If the version in the destination is newer, then the files will not be overwritten. Also, all other new files or directories from /source/directory/ will be copied to /destination/.
To copy an existing file and delete a file that does not exist in the destination directory:
>rsync -av --delete /source/directory/ /destination/directory/
In this scenario, both the source and destination directories contain example1.txt and example2.txt. The /destination/directory contains an extra file, example3.txt.
The rsync command copies or updates the files example1.txt and example2.txt from /source/directory/ to /destination/directory/. The command also deletes the file example3.txt from /destination/directory/ as it does not exist in the /source/directory/. Also, the directory structure in /destination/directory/ is cleaned up to match the /source/directory/.
7 Troubleshooting rsync #
File or folder copying may fail due to several reasons. Ensure that you have the required permissions and resources to perform the file copy.
Possible troubleshooting methods are mentioned below:
Ensure that you have the latest version of rsync. To transfer files or directories remotely, ensure that rsync is installed on both the source and destination machines.
Ensure that you have the required rsync access permission.
Ensure that you provide the correct host name to copy files remotely.
Ensure that the rsync service is running if you are using rsync in daemon mode.
8 Legal Notice #
Copyright© 2006–2025 SUSE LLC and contributors. All rights reserved.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or (at your option) version 1.3; with the Invariant Section being this copyright notice and license. A copy of the license version 1.2 is included in the section entitled “GNU Free Documentation License”.
For SUSE trademarks, see https://www.suse.com/company/legal/. All other third-party trademarks are the property of their respective owners. Trademark symbols (®, ™ etc.) denote trademarks of SUSE and its affiliates. Asterisks (*) denote third-party trademarks.
All information found in this book has been compiled with utmost attention to detail. However, this does not guarantee complete accuracy. Neither SUSE LLC, its affiliates, the authors, nor the translators shall be held liable for possible errors or the consequences thereof.