How to synchronize files and folders on Linux

Linux administrators must keep folders synced on a regular basis. There are two simple use scenarios for synchronizing these files and folders.

Linux administrators must keep folders synced on a regular basis. There are two simple use scenarios for synchronizing these files and folders. First, an important data backup needs to be maintained continuously at another location (local or remote). This backup runs at regular intervals. In other cases, the web server's data is copied to another server, for download sharing or for backup purposes. In both cases, there is a source point and a destination point needs to be synchronized.

Linux has powerful tools that allow users to synchronize files or folders from one source folder to another. However, the preferred tool for doing this is 'rsync'.

To do this, please follow these instructions:

Step 1 : Install the inotify tool

 sudo apt-get install inotify-tools 

Details can be found on github.com/rvoicilas/inotify-tools/wiki.

Step 2 : Install rsync

 sudo apt-get install ssh rsync 

Step 3 : Create a new bash script file named sync.sh

 nano sync.sh 

Step 4 : Paste the code below into sync.sh file :

 #!/bin/sh while inotifywait -r -e modify,create,delete,move /home/amrit/rsync-test/source_folder; do rsync -avz /home/amrit/rsync-test/source_folder /home/amrit/rsync-test/destination_folder --delete done 

The user must modify the destination directory and source in the shell command above to match his setup.

Step 5 : Execute the shell script file:

 chmod +x sync.sh 

Step 6 : Run the shell script:

 ./sync.sh 

Mission completed! At this point if any changes to the source directory are made, those changes will also apply to the destination directory.

Hope you are succesful.

See more:

  1. 7 commands to manipulate the most basic files and folders everyone must know
  2. Learn the file system and folders on Linux operating systems
  3. File Management in Unix / Linux
4.8 ★ | 5 Vote