In Linux, “rsync” stands for Remote Sync, a method for the synchronization of files and directories that works both remotely or locally. The “rsync” works with the algorithm of “Delta” that minimizes the amount of copied data by only shifting the changed data of files and directories.
This article will demonstrate the possible method and examples to rsync a directory in Linux.
- What is the rsync Command in Linux?
- How to rsync a Directory in Linux?
What is the rsync Command in Linux?
The rsync is the built-in network-enabled utility that lets users sync the directories and files. The syntax for using the “rsync” is given below:
Syntax:
$ rsync [Options] [Source_Dir] [Dest_Dir]
Type the “rsync” keyword, “options,” along with the rsync command, and then type the name of the destination and source directories.
For more details on syntax and options can be observed using the command:
$ rsync --help
How to rsync a Directory in Linux?
To understand the method of syncing a directory, let’s create two directories (Dir1 and Dir2) for testing purposes:
$ mkdir Dir1
$ mkdir Dir2
Both directories have been created.
Now, let’s put some test files in the “Dir1,” which will be synced in the “Dir2”. To do so, use the “touch” command with the directory’s name, and specify the number of testing files with its name:
$ touch Dir1/testfile{1..30}
The test files will be created in the specified directory. Let’s check it by running the “ls” command:
$ ls Dir1
The “Dir1” contains the 30 test files. Now, our Directory is ready to be synced.
Example 1: rsync Directory Recursively
To rsync the directory, use the “r” flag, which is essential when syncing a directory. Furthermore, it also tells the command that rsync is recursive:
$ rsync -r Dir1/ Dir2
The directory “Dir1” will be synced to the “Dir2”.
Let’s check the output of the “Dir2” using the “ls” utility:
$ ls Dir2
All the files of “Dir1” will have been synced to “Dir2”.
Example 2: Maintaining File Permissions, Symbolic Links, and Modification Time
To rsync a directory for preserving the file permissions, symbolic link, and modification time for all the files that are present in the directory, we will use the “a” flag with “rsync” command:
$ rsync -a Dir1/ Dir2
The successful execution of the command shows that the directory “Dir1” is synced to the “Dir2”.
Now, let’s check the permissions and modification time for the directory:
$ ls -al ./Dir2/
The above shows the file permissions and modification time for the directory, which is the same as the files for the “Dir1” directory.
Example 3: Displaying rsync Directory on the Terminal
To display the sync process on the terminal, use the “a”, “n,” and “v” options:
$ rsync -anv Dir1/ Dir2
The details of the rsyncing directory will be printed on the screen.
Note: Putting the “/” in the source directory will sync the content of the directory, whereas without “/” it will sync the directory to another. Check the below execution of the command:
The above image shows that without “/,” the directory “Dir1” itself synced to the “Dir2” Directory.
Example 4: rsync Directory with Progress Bar
To rsync, the directory with the progress bar is obtained as follows:
$ rsync -azP Dir1 Dir2
The directory will be synced with the progress bar.
Example 5: rsync Directory Remotely
To rsync a directory remotely, the following syntax is utilized:
Syntax:
$ rsync -a [~/Source_Directory] [username@Host_name]:[Destination_Directory]
Use the above syntax, provide the source_directory, hostname, and destination directory for syncing the directory remotely.
Conclusion
To rsync a directory in Linux, use the “rsync” utility with the various options which depend upon the user’s requirements. These options, such as rsync a directory remotely, recursively, preserving all the permissions and modification time, and with the progress bar, have been explained. This write-up has illustrated the method to rsync a directory in Linux.