How to Use rsync Dry Run?

The rsync command in Linux shares the file’s data to local and remote machines fastly and efficiently. The rsync command-line tool is used for performing a backup or transferring/copying data to different servers locally as well as remotely. Sometimes, you just want to test the output of the command instead of performing the task directly, that’s where the “dry-run” function of the rsync is used.

The dry-run option of the rsync command in Linux allows the user to test the command output without sharing the system files, which will be discussed throughout this guide with the following timeline:

Let’s get started.

What is rsync dry run?

The rsync in Linux-based systems is a synchronization utility that syncs the files and transfers data between local and remote servers. The rsync makes copying and transferring data from one host to another fast and user-friendly. The rsync command comes with several features, one of the important features is its “dry-run” option.

The “dry-run” allows the rsync command to test without altering anything, and it generates the same output as a real-time rsync command. The dry-run option of the rsync command is mostly used in with the -v option which shows the details about the output of the rsync dry-run command. The output of –itemize-changes displays exactly the same output as the dry run and its real execution. If the “dry-run” and “itemize-changes” results are different, that’s a bug. Other outputs should be mostly unchanged.

How to use rsync dry-run?

The rsync command is commonly used as the dry run for testing purposes which provides the same output as in the case of the actual execution of the rsync command. The rsync command can be used with all its options for the dry run.

The only difference between the actual running of the rsync command and the dry run rsync command is the use of the “–dry-run” option in the command otherwise, the output will be the same. This section will implement the dry run of the rsync command on local and remote servers, along with different options.

Use rsync dry-run to Sync Files to the Local Server

The rsync command can be utilized for data sharing of remote or local servers. To transfer/sync a file “testfile1.txt” locally for a dry run to another directory “TestDir2”, the following command is run:

$ rsync -v --dry-run TestDir1/testfile1.txt TestDir2

To sync or copy a directory on a local server with the dry-run option, the below command is utilized:

$ rsync -v --dry-run TestDir1 TestDir2

The “r” option of the rsync command checks for syncing the directory data recursively until all the files are synced. To check for the data recursively for TestDir1 to sync with TestDir2, utilize the following command:

$ rsync -rv --dry-run TestDir1 TestDir2

The “a” (archive mode) option allows the users to sync the data without syncing the important information, such as file permissions and ownership. To sync as a dry run for the “testfile2.txt” using the archive mode, run the below command:

$ rsync -av --dry-run TestDir1/testfile1.txt TestDir2

The “–progress” option of the rsync command shows the progress of the transfer files data. For instance, the below command tracks the sync directory “TestDir1” progress:

$ rsync -av --progress --dry-run TestDir1 TestDir2

We can use the “max-size” option to transfer only files less than the specified size. For instance, the below command syncs the command will sync those files only whose size is less than “500KB”:

$ rsync -av --max-size=500k --dry-run TestDir1 TestDir2

We can also sync those files only having a size greater than a specified size with the “min-size” option. For example, the below command will dry run the sync command to sync only those files which have a size of more than “20KB”:

$ rsync -av --min-size=20k --dry-run TestDir1 TestDir2

The output shows that only 1 file is synced whose size is more than “20KB”.

Use rsync dry-run to Sync Files to Remote Servers

We can sync the file to remote servers using the rsync command, which will be discussed as a dry run in this article. For example, the below command will copy the “testfile1.txt” to the remote server directory “Documents”:

$ sudo rsync -v --dry-run TestDir1/testfile1.txt 192.168.100.46:/home/ubuntu/Documents

To copy the directory “TestDir1” to remote server directory “Documents”, use the below command:

$ sudo rsync -v --dry-run TestDir1 192.168.100.46:/home/ubuntu/Documents

Use rsync dry-run with Delete Option

The rsync command can be used with the “delete” option to delete the files from the destination directory which is not present in the synced source directory. The below command will delete the files from the TestDir2 not present in the TestDir1:

$ rsync -av --delete --dry-run TestDir1 TestDir2

Use rsync dry-run with remove-source-files Option

The “remove-source-files” option is useful in deleting the source files after successfully transferring the files. For instance, the below will delete the “testfile1.txt” from the source directory after copying to the “TestDir2” as shown below:

$ rsync -av --remove-source-files --dry-run TestDir1/testfile1.txt TestDir2

The below command will remove the “TestDir1” from the source after transferring it to other directory TestDir2:

$ rsync -av --remove-source-files --dry-run TestDir1 TestDir2

Use rsync dry-run with head and tail Options

The head and tail commands are used with the rsync command as a dry-run to sort the data in a specific manner. For example, the following command will copy the TestDir1 to the TestDir2 and shows the data of the top 3 lines of the output:

$ rsync -av --dry-run TestDir1 TestDir2 | head --lines=-3 | tail --lines=+3

Similarly, we can sort the output of the rsync command and store its output in the file “output.txt”, using the following command:

$ rsync -av --dry-run TestDir1 TestDir2 | head --lines=-3 | tail --lines=+3 > output.txt
$ cat output.txt

The cat command output shows that the sorted data is stored in the “output.txt” file.

That’s how a rsync dry run is performed.

Conclusion

The rsync command in Linux syncs or copies the data between the remote and local servers, which can be tested as a dry run to check the output. The “dry-run” option uses the rsync command as a trial without changing the system. The rsync command can be used as a dry-run to share data to remote or local servers. Moreover, the “dry-run” option can be used with the “delete”, “remove-source-files”, “head” and “tail” options to test the output of the rsync command.