A simple bash script which lets you automatically synchronize one or more directories to a target folder whenever files in the watched directories change.
This is a much more primitive variant of the lsyncd tool.
Unlike lsyncd
though, this script can also run in Cygwin terminal or in Git Bash on Windows.
- Download the
autosyncdir.sh
script to any directory on your PC. You can also do that by Git-cloning the whole project. - The script relies on
inotifywait
andrsync
. Parent directories of these programs need to be in the system PATH (see script's source code for more details). - Put the script's directory to the system PATH, so that you can run it easily just by its name.
Simply run the script without any parameters in order to get the basic usage help.
Press Ctrl+C at any time to terminate the script.
On Windows, you will typically run the script from Git Bash ("a customized distro of MSYS2"; part of Git for Windows), or Cygwin terminal.
You can run the script directly for an ad-hoc usage.
On Windows:
$ autosyncdir.sh 'c:\path\to\source_dir1\' 'c:\path\to\source_dir2\' 'c:\path\to\target_dir'
$ # an alternative with forward slashes:
$ # autosyncdir.sh c:/path/to/source_dir1/ c:/path/to/source_dir2/ c:/path/to/target_dir
# Starting the sync loop
# Syncing...
sending incremental file list
sent 98 bytes received 12 bytes 220.00 bytes/sec
total size is 13 speedup is 0.12
# Waiting for changes...
===> Watching c:\path\to\source_dir1\ -r*.* for create, modify, delete, move
===> Watching c:\path\to\source_dir2\ -r*.* for create, modify, delete, move
On Linux:
$ autosyncdir.sh /path/to/source_dir1/ /path/to/source_dir2/ /path/to/target_dir
# Starting the sync loop
# Syncing...
sending incremental file list
sent 98 bytes received 12 bytes 220.00 bytes/sec
total size is 13 speedup is 0.12
# Waiting for changes...
===> Watching /path/to/source_dir1/ -r*.* for create, modify, delete, move
===> Watching /path/to/source_dir2/ -r*.* for create, modify, delete, move
Usually, you will want to prepare another script where you can persist all what you need to synchronize.
Here is an example of such a script (name it e.g. sync_my_dirs.sh
).
On Windows:
#!/bin/bash
# Starts to automatically watch and sync all my directories.
server_cfg='c:\server1\cfg'
autosyncdir.sh 'c:\project1\env\devel\' $server_cfg'\project1' &
autosyncdir.sh 'c:\project2\env\devel\' $server_cfg'\project2' &
wait
On Linux:
#!/bin/bash
# Starts to automatically watch and sync all my directories.
server_cfg=~/server1/cfg
autosyncdir.sh ~/project1/env/devel/ $server_cfg/project1 &
autosyncdir.sh ~/project2/env/devel/ $server_cfg/project2 &
wait
You can override options which are passed by the script to inotifywait
and rsync
by setting environment variables as in the following example.
#!/bin/bash
# default: "-r" (recursive)
export INOTIFYWAIT_OPTS=" "
# WARNING: Be careful with the --delete option, as it will delete missing files in the target directory!
# default: "-avz" (archive, verbose, compress)
export RSYNC_OPTS="-avz --delete"
autosyncdir.sh /path/to/source_dir /path/to/target_dir