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. 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:
- 7 commands to manipulate the most basic files and folders everyone must know
- Learn the file system and folders on Linux operating systems
- File Management in Unix / Linux
You should read it
- How to use Zsync to transfer a file part in Linux
- Useful commands in Unix / Linux
- 14 interesting Linux commands in Terminal
- Basic Linux commands everyone needs to know
- How to manage photos on Linux
- 5 Linux commands every sysadmin needs to know
- Guide to network operation for Linux users: 11 commands to know
- Convert data, applications from Windows to Linux in minutes
- How to Run Linux Commands on Windows with WSL 2
- Manage folders in Unix / Linux
- Synchronize cloud storage services on Linux with Rclone
- File Management in Unix / Linux