DEV Community

Cover image for Using the Rsync Command for Copying and Synchronizing Files and Directories
Javier Gongora
Javier Gongora

Posted on

Using the Rsync Command for Copying and Synchronizing Files and Directories

Rsync is a powerful Linux/Unix command that allows you to copy and synchronize files and directories between local and remote systems. It is particularly useful for synchronizing large numbers of files, as it is efficient and only transfers the differences between the source and destination files.

Here's a brief overview of how to use the Rsync command:

Copying files and directories locally

To copy a file or directory from one location to another on the same system, you can use the following syntax:

rsync <source> <destination>

For example, to copy a file named file.txt from the current directory to the /tmp directory, you could use the following command:

rsync file.txt /tmp

To copy a directory and its contents, you can add the -r flag to the command:

rsync -r <source> <destination>

For example, to copy the /var/www/html directory and its contents to the /tmp directory, you could use the following command:

rsync -r /var/www/html /tmp

Copying files and directories remotely

To copy a file or directory from a remote system to a local system, you can use the following syntax:

rsync <user>@<remote_host>:<source> <destination>

For example, to copy a file named file.txt from the /tmp directory on a remote system with hostname example.com to the current directory on your local system, you could use the following command:

rsync user@example.com:/tmp/file.txt .

To copy a directory and its contents, you can add the -r flag to the command as before:

rsync -r <user>@<remote_host>:<source> <destination>

For example, to copy the /var/www/html directory and its contents from a remote system with hostname example.com to the /tmp directory on your local system, you could use the following command:

rsync -r user@example.com:/var/www/html /tmp

Synchronizing files and directories

To synchronize files and directories between two systems, you can use the -a flag, which stands for "archive" and preserves the file attributes and permissions. You can also use the -u flag, which stands for "update" and only transfers files that are newer on the source system:

rsync -au <user>@<remote_host>:<source> <destination>

For example, to synchronize the /var/www/html directory and its contents from a remote system with hostname example.com to the /tmp directory on your local system, making sure to only transfer newer files and preserve file attributes and permissions, you could use the following command:

rsync -au user@example.com:/var/www/html /tmp

Whether you're looking to simply copy files from one location to another, or synchronize entire directories between systems, Rsync can be a valuable tool to have in your toolkit. So give it a try and see how it can streamline your file management processes.

Credits:
Image by storyset on Freepik

Top comments (0)