Fswatch is a free, open source multi-platform file change monitor utility that notifies us when the contents of the specified files or directories are modified or changed. Using fswatch, we can easily monitor the changes being made in files and/or directories. It supports all operating systems, including GNU/Linux, *BSDs, Mac OS X, Solaris, and Microsoft Windows etc. In this brief guide, let me show you how to monitor file changes using fswatch in Unix-like operating systems.
Types of monitoring
fswatch implements the following types of monitors.
- A monitor based on the File System Events API of Apple OS X.
- A monitor based on kqueue, a notification interface introduced in FreeBSD 4.1.
- A monitor based on the File Events Notification API of the Solaris kernel and its derivatives.
- A monitor based on inotify, a Linux kernel subsystem that reports file system changes to applications.
- A monitor based on ReadDirectoryChangesW, a Microsoft Windows API that reports changes to a directory.
- A monitor which periodically stats the file system, saves file modification times in memory, and manually calculates file system changes.
Concerning about the features of fswatch, we can list the following:
- Cross-platform and open source utility.
- Support for many OS-specific APIs.
- Recursive directory monitoring.
- Path filtering using including and excluding regular expressions.
- Customizable record format.
- Support for periodic idle events.
- And many.
Install fswatch In Linux
Since the fswatch utility is not available in the default repositories of most Linux distributions, you need to manually compile and install the latest version from the source as described below.
Before compiling, you need to install Development tools in your Linux distribution. To install Development tools on various Linux distributions, refer the following guide.
Then, download the fswatch source file from here.
$ wget https://github.com/emcrisostomo/fswatch/releases/download/1.9.3/fswatch-1.9.3.tar.gz
Extract the downloaded tarball:
$ tar -zxvf fswatch-1.9.3.tar.gz
Go to the project's folder:
$ cd fswatch-1.9.3/
Finally, compile and install fswatch by running the following commands one by one.
$ sudo make install
Finally, run the following command to refresh the links and cache to the dynamic libraries:
$ sudo ldconfig
If you don't run the above command, you might get the following error in GNU/Linux systems.
fswatch: error while loading shared libraries: libfswatch.so.6: cannot open shared object file: No such file or directory
Monitor File Changes Using fswatch In Linux
Usage of fswatch is no big deal. The typical syntax of fswatch is:
$ fswatch [options] ... path-0 ... path-n
To test how fswatch works, open two Terminal windows (Let us call them Terminal 1 and Terminal 2).
In Terminal 1, run the fswatch command to monitor the $HOME directory.
$ fswatch /home/sk/
And, in the Terminal 2 do some operations such as creating files/folders, deleting files, modifying files etc.
Whatever you do in the terminal 2 will be notified on the Terminal 1. Have a look at the following screenshots.
Terminal 1 - fswatch command is running and the file changes are being monitored:
Terminal 2 - Do some random changes in files/folders:
By default fswatch will choose the best monitor available on the current platform, in terms of performance and resource consumption. In Linux, the default monitor is inotify.
To list the available monitors in the current platform (i.e Linux in our case), run:
$ fswatch -M
$ fswatch --list-monitors
To monitor a specific file or directory with a particular monitor option, run:
$ swatch -m kqueue_monitor /home/sk/
By default, fswatch will keep monitoring the file changes until you manually stop it by invoking CTRL+C keys.
You can also exit fswatch after the first set of events is received by specifying the option -1 as shown in the following command:
$ fswatch -1 /home/sk/
This command will exit just after the first set of events is received.
fswatch will monitor changes in all files/folders in the specified path. If you want to watch the changes made in the directories only, use -d option.
$ fswatch -d /home/sk/
Of course, there are more options. Refer the man pages or the project's documentation page for detailed instructions.
$ man fswatch