Unix / Linux: Sync Local Directories

To sync files between two local directories on Unix or Linux, you can either choose to perform a soft link to the source directory or use rsync or lsyncd daemon.

Let’s say that you have a source directory /mnt/source. And you want all the files to be synced from /mnt/source to /mnt/dest directory.

Sync with soft link

Technically, this method does not sync the files. It merely creates a symbolic link to the source directory.

That’s it. All the files that you see on /mnt/source will now also be available under /mnt/dest. But take note that if you accidentally delete a file from the source directory, you will not be able to recover the file from the destination directory. You can use this method if you just need the same files to appear on two different directories.

Sync with rsync

The rsync tool will allow you to make a proper sync of two directories. Install rsync if it’s not yet available on your system.

Once installed, perform a sync by using the rsync command. The “-a” option will preserve the file permission, ownership and timestamps.

To perform the sync at periodic intervals, you can consider scheduling rsync from cron.

Sync with lsyncd

lsyncd daemon monitors the filesystem for changes using inotify. To know more about inotify, refer to Linux: How to Monitor File System for Changes. Install lysncd daemon on your system.

Next, edit the lsyncd configuration file lsyncd.conf.lua found in /etc/lsyncd. Some distributions keep the file under the /etc directory.

Once the config file has been modified, restart the lsyncd daemon for the changes to take effect.

If there are no errors, the lysncd daemon will start to continuously monitor the source directory for changes and the destination directory will be updated within a few seconds without any manual intervention. Do not use lsyncd to perform backups as once the source file is removed, lsyncd will automatically remove the corresponding file in the destination directory as well.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }