Install Rsync Utility
Before going to the implementation, we need to make sure that the “rsync” utility is already installed and ready for use. Thus, we have been using the “apt” install instruction at the console application query area. The execution of this instruction asks for the currently logged-in user account password. After this, the system will install the rsync utility in a few moments. The output shows that the rsync utility is already installed in our system and ready for use.
Before using the rsync query in the shell, we will take a look at its help guide by the use of the “—help” option in the query area. The “help” guide can let us see its installed version, along with its capabilities mentioned in the output as presented in the image.
Scroll a little down in its shown output, and you will be presented by the use of the rsync utility. It shows the syntax to be used for this instruction on the shell and explains all of its options in the “Options” section as per the output below.
Copy Single File Locally
Let’s say you have a file named “fall.sh” within the current working directory of your system, i.e., the home directory. We have tried the “ls” instruction to list down all the files and folders of our Linux system’s current directory and got the shown-below output, i.e., the fall.sh file is also listed.
We have tried the rsync instruction with the sudo rights and “-a” option to copy the file “fall.sh” from the current working directory (home/saeedraza) to the “/tmp” folder of our Linux system in an archive format. The command doesn’t output anything. So, we have tried the “cd” instruction along with the path “/tmp” to move within the “tmp” folder and tried the “ls” list instruction to display the data of the “tmp” folder. It displayed that the fall.sh file has been successfully moved from one folder to another folder working locally.
So, we have tried the very same instruction to move the fall.sh file in the “tmp” folder with a new name, “new.sh”. After moving to the “tmp” folder with the “cd” instruction and listing the contents of the “tmp” folder, we can see that the folder has a new file “new.sh,” i.e., a file “fall.sh”.
Copy Multiple Files Locally
We will be using the rsync instruction to copy multiple files from one local folder to another. So, we have tried the “ls” instruction to find out the current contents of a current working directory. We found that it has two bash files, i.e., fall.sh and new.sh. Make sure to remove the old copied files from the “tmp” folder to successfully copy multiple files in the same folder.
Thus, we have tried the same rsync instruction with sudo rights and the “-v” option for the verbose format of files. After that, we have added the path to both files to copy both the files from the current working directory “home” to the new folder “tmp”. It requires the “sudo” password and successfully copied both the files in the destination path, i.e., “/tmp”. It displayed the number of data bytes delivered, received, total size, and speed of delivery. After this, we tried the “cd” instruction to move within the “tmp” folder and listed its data. Both the files are presented in the “tmp” folder of this system as per the yielded result.
Copy a Directory Locally
We can move a whole directory from one main folder to another. So, we have created a new directory named “move” within our current home folder by the use of the “mkdir” instruction.
Now, we have been using the rsync command statement in Ubuntu’s shell with the sudo rights and “-a” option to make the archive of a “move” folder into the “/tmp” folder of our Ubuntu 20.04 system. For this, you need to add the path to both the source and destination. The tmp folder has been showing the “move” folder listed in it.
We can also move the folder to its destination folder with a new name as we did for files. So, we have tried the same rsync instruction and moved the “move” folder to the “tmp” folder with a new name, “new”. The “ls” instruction shows the success of the “rsync” command.
Copy a Directory With Sub-directories Locally
In this illustration, you will see the use of rsync to copy the folder and its subfolders locally. We have created a new folder, “test,” with the “mkdir” instruction.
Created three subfolders within the newly created “test” folders.
Used the “rsync” instruction with the “-av” option to move the “test” folder to a “new” folder. The output has been showing that the “test” folder along with its subfolders are copied.
Moving within the “new” folder, we have found that the rsync command was successful.
Copy a Directory Remotely
To move files from source to destination remotely, we need the ip address of a particular destination. The below-shown command is copying the “new” folder to the other virtual machine with a specific ip address.
To copy a file from a remote machine to a local machine, you need to exchange the position of paths in the rsync command as presented.
We have discussed the installation and usage of the rsync command in Ubuntu 20.04. After this, we have discussed the rsync instruction usage within the command shell to copy a file from one folder to another locally, copy multiple files, copy a folder along with its subfolders, and the use of rsync to copy files from local to remote and remove to the local platform.