Rsync examples

By | March 15, 2023
Rsync examples

Rsync is a powerful command-line tool used for synchronizing files and directories between two different locations on a computer or over a network. It uses a delta-transfer algorithm to transfer only the differences between the source and destination files, making it an efficient and fast way to transfer files.

In this article, we will explore some common rsync examples and how to use them effectively.

Copy a directory

To copy a directory and its contents from one location to another, use the following command:

rsync -av /path/to/source/directory/ /path/to/destination/directory/

The -a flag indicates archive mode, which preserves all file attributes and permissions during the transfer. The -v flag enables verbose output, so you can see which files are being transferred.

For example, to copy the contents of the ~/Documents directory to a backup location at ~/backup/Documents, run the following command:

rsync -av ~/Documents/ ~/backup/Documents/
Rsync examples
Rsync examples

Copy only new or modified files

If you want to synchronize only the new or modified files between two directories, you can use the --update or -u option. This option transfers only files that are newer or have been modified since the last synchronization.

rsync -avu /path/to/source/directory/ /path/to/destination/directory/

For example, to update a backup directory with new or modified files from the source directory, run the following command:

rsync -avu ~/Documents/ ~/backup/Documents/


Exclude specific files or directories
Sometimes, you may want to exclude specific files or directories from the rsync transfer. You can do this by using the --exclude or -exclude-from option.

For example, to exclude all .git directories and .log files from the synchronization, run the following command:
rsync -av --exclude='.git/' --exclude='*.log' /path/to/source/directory/ /path/to/destination/directory/

You can also create a text file that lists the files or directories you want to exclude and use the -exclude-from option to specify it:

rsync -av --exclude-from='exclude-list.txt' /path/to/source/directory/ /path/to/destination/directory/


Rsync is a powerful command-line tool used for synchronizing files and directories between two different locations on a computer or over a network. It uses a delta-transfer algorithm to transfer only the differences between the source and destination files, making it an efficient and fast way to transfer files.

In this article, we will explore some common rsync examples and how to use them effectively.

Copy a directory
To copy a directory and its contents from one location to another, use the following command:

javascript
Copy code
rsync -av /path/to/source/directory/ /path/to/destination/directory/
The -a flag indicates archive mode, which preserves all file attributes and permissions during the transfer. The -v flag enables verbose output, so you can see which files are being transferred.

For example, to copy the contents of the ~/Documents directory to a backup location at ~/backup/Documents, run the following command:

javascript
Copy code
rsync -av ~/Documents/ ~/backup/Documents/
Copy only new or modified files
If you want to synchronize only the new or modified files between two directories, you can use the --update or -u option. This option transfers only files that are newer or have been modified since the last synchronization.

javascript
Copy code
rsync -avu /path/to/source/directory/ /path/to/destination/directory/
For example, to update a backup directory with new or modified files from the source directory, run the following command:

javascript
Copy code
rsync -avu ~/Documents/ ~/backup/Documents/
Exclude specific files or directories
Sometimes, you may want to exclude specific files or directories from the rsync transfer. You can do this by using the --exclude or -exclude-from option.

For example, to exclude all .git directories and .log files from the synchronization, run the following command:

javascript
Copy code
rsync -av --exclude='.git/' --exclude='*.log' /path/to/source/directory/ /path/to/destination/directory/
You can also create a text file that lists the files or directories you want to exclude and use the -exclude-from option to specify it:

javascript
Copy code
rsync -av --exclude-from='exclude-list.txt' /path/to/source/directory/ /path/to/destination/directory/
Transfer files over SSH
You can use rsync to transfer files securely over SSH. To do this, you need to specify the SSH connection details using the -e option.

rsync -av -e ssh /path/to/source/directory/ user@remotehost:/path/to/destination/directory/

For example, to transfer files from a local directory to a remote server over SSH, run the following command:

rsync -av -e ssh ~/Documents/ user@remotehost:~/backup/Documents/


Dry run mode

Sometimes, you may want to test your rsync command without actually transferring any files. You can use the --dry-run or -n option to simulate the transfer and see which files would be transferred.

rsync -avn /path/to/source/directory/ /path/to/destination/directory/


For example, to test the rsync command that copies files from ~/Documents to ~/backup/Documents, run the following command:





rsync -avn ~/Documents/ ~/backup/Documents/






Conclusion



Rsync is a powerful and versatile tool for synchronizing files and directories between two locations. By using the examples in this article, you can become more efficient in managing and transferring your files. Remember to use the appropriate flags and options based on your specific requirements, and