How to remove source files after transfer using rsync

7

Rsync is a very popular command used in Linux for syncing files or directories either locally or remotely. The reason behind its popularity is that it only takes the changes and copies them to the destination. Mainly, this command is used in keeping the data backup and restoration. This post will learn a bit about the rsync command, how it works, its syntax, and how to remove the source files after transfer using rsync.

Syntax

The syntax of writing and executing the rsync command is really simple.

$ rsync -options SOURCE_PATH DESTINATION_PATH

In the above-given syntax, we first have to provide the options to the rsync command and later provide the source from where you want to transfer the data or files and destination path.

The most common options used in the rsync command are as follows:

-a option is for archive files and folders during synchronization,
-r is for synchronizing the files in recursive mode,
-v is for verbose output,
-b is for keeping the backup during the synchronization,
-z is for compressing and transfer the files,
-progress is for showing progress.

Installation of rsync command

Usually, the rsync command is already available in all Linux-based operating systems, but it does not come pre-installed in the minimal installation of an Operating system.

To install rsync in Ubuntu or any Debian based Operating system, use the command given below:

$ sudo apt install rsync -y

To install rsync in CentOS or RHEL based Operating system, use the command given below:

$ sudo dnf install rsync -y

Rsync Command syntax for remote transfer

The syntax of the rsync command for remote transfer is a bit different. We have to provide the username and the IP address of the remote host where we want to transfer files using the syntax provided below:

Local to Remote Transfer

For transferring files from the local system to a remote system, you need to use the syntax given below:

$ rsync -options SOURCE_PATH @:DESTINATION_PATH

Remote to Local

For transferring files from any remote host to the local system, you need to use the syntax given below:

$ rsync -options @:SOURCE_PATH DESTINATION_PATH

Now, you have all the basic and required knowledge of the rsync command, let’s understand how to remove the source files after transfer.

How to remove source files after transfer

For removing the files from the source after the transfer, the rsync command provides the –remove-source-files option.

Examples

Based on the files or directory transfer, the options may vary. So we will first look at an example of how to transfer files and then transfer a directory using the rsync command.

For transferring a single file
Suppose we want to transfer an mp3 file to the documents directory, and after transferring, we want to remove the source file.

The command for transferring and removing the mp3 file after transfer would go like this:

$ rsync –remove-source-files -zvh Music/audio.mp3 Documents/

In the above command, we used the -zvh options:

-z for compressing the file and then transfer
-v for verbose output
-h for displaying the output in human-readable format

After executing the above-given command, if we execute the ls command in the home directory:

You can see that the mp3 file is successfully removed from the source after transfer using the rsync command.

For transferring a directory
For example, we want to transfer a directory named music, which includes a couple of mp3 files in it as well, to the Documents directory, and after transferring, we want to remove the music directory.

The command for transferring and removing the music directory after transfer would go like this:

$ rsync –remove-source-files -zavh /home/music /home/Documents

In the above command, we used the -zavh options:

-z for compressing the file and the transfer
-a for archiving the whole directory and transfer
-v for verbose output
-h for displaying the output in human-readable format

After executing the above-given command, if we execute the ls command in the home directory:

You can see that in the music directory, everything is successfully removed from the source after transfer using the rsync command.

Conclusion

Rsync is a command used for transferring files or directories from one place to another efficiently by just transferring the changes instead of the whole data again and again while back up or mirroring operations. This post provides the basic syntax, its usage, and installation method in any Linux-based Operating system. This post provides two different examples of transferring a file or directory using rsync and removing the source files after transfer.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More