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 Linux and 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
The fswatch utility is available in the default repositories of popular Linux distributions.
To install fswatch in Debian, Ubuntu, Linux Mint, Pop OS, and other APT-based systems, run:
$ sudo apt install fswatch
To install fswatch in Fedora, RHEL, CentOS, AlmaLinux and Rocky Linux, enable [EPEL] repository using command:
$ sudo dnf install epel-release
And then install install fswatch using command:
$ sudo dnf install fswatch
To install fswatch in openSUSE, run:
$ sudo zypper install fswatch
Install fswatch from source
If fswatch is not available for your distribution, you can 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.17.1/fswatch-1.17.1.tar.gz
Extract the downloaded tarball:
$ tar -zxvf fswatch-1.17.1.tar.gz
Go to the project's folder:
$ cd fswatch-1.17.1/
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
Finally, check the fswatch version to make sure if it is installed correctly:
$ fswatch --version fswatch 1.17.1 Copyright (C) 2013-2021 Enrico M. Crisostomo <firstname.lastname@example.org>. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Enrico M. Crisostomo.
Install fswatch in FreeBSD
On FreeBSD, fswatch can be installed using
# pkg install fswatch-mon
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
$ fswatch /home/ostechnix/
And, in the Terminal 2 do some operations such as creating files/folders, deleting files, and 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.
By default, fswatch will keep monitoring the file changes until you manually stop it by invoking
To list the available monitors in the current platform (i.e Linux in our case), run:
$ fswatch -M
$ fswatch --list-monitors
Monitor specific file or folder
To monitor a specific file or directory with a particular monitor option, run:
$ swatch -m kqueue_monitor /home/ostechnix/
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/ostechnix/
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
$ fswatch -d /home/ostechnix/
Of course, there are more options. Refer the man pages or the project's documentation page for detailed instructions.
$ man fswatch