Fswatch
A cross-platform file change monitor with multiple backends: Apple macOS File System Events, *BSD kqueue, Solaris/Illumos File Events Notification, Linux inotify, Microsoft Windows and a stat()-based backend.
Install / Use
/learn @emcrisostomo/FswatchREADME
README
fswatch is a file change monitor that receives notifications when the contents
of the specified files or directories are modified. fswatch implements
several monitors:
- A monitor based on the File System Events API of Apple macOS.
- A monitor based on kqueue, a notification interface introduced in FreeBSD 4.1 (and supported on most *BSD systems, including macOS).
- 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 (which works
anywhere
stat (2)can be used).
fswatch should build and work correctly on any system shipping either of the
aforementioned APIs.
Table of Contents
- libfswatch
- Features
- Limitations
- Getting fswatch
- Building from Source
- Installation
- Documentation
- Localization
- Usage
- Contributing
- Bug Reports
libfswatch
fswatch is a frontend of libfswatch, a library with C, C++ and
Go bindings. More information on libfswatch can be found
here.
Features
fswatch main features are:
- Support for many OS-specific APIs such as kevent, inotify, and FSEvents.
- Recursive directory monitoring.
- Path filtering using including and excluding regular expressions.
- Customizable record format.
- Support for periodic idle events.
Limitations
The limitations of fswatch depend largely on the monitor being used:
-
The FSEvents monitor, available only on macOS, has no known limitations, and scales very well with the number of files being observed.
-
The File Events Notification monitor, available on Solaris kernels and its derivatives, has no known limitations.
-
The kqueue monitor, available on any *BSD system featuring kqueue, requires a file descriptor to be opened for every file being watched. As a result, this monitor scales badly with the number of files being observed, and may begin to misbehave as soon as the
fswatchprocess runs out of file descriptors. In this case,fswatchdumps one error on standard error for every file that cannot be opened. -
The inotify monitor, available on Linux since kernel 2.6.13, may suffer a queue overflow if events are generated faster than they are read from the queue. In any case, the application is guaranteed to receive an overflow notification which can be handled to gracefully recover.
fswatchcurrently throws an exception if a queue overflow occurs. Future versions will handle the overflow by emitting proper notifications. -
The Windows monitor can only establish a watch directories, not files. To watch a file, its parent directory must be watched in order to receive change events for all the directory's children, recursively at any depth. Optionally, change events can be filtered to include only changes to the desired file.
-
The poll monitor, available on any platform, only relies on available CPU and memory to perform its task. The performance of this monitor degrades linearly with the number of files being watched.
Usage recommendations are as follows:
-
On macOS, use only the
FSEventsmonitor (which is the default behaviour). -
On Solaris and its derivatives use the File Events Notification monitor.
-
On Linux, use the
inotifymonitor (which is the default behaviour). -
If the number of files to observe is sufficiently small, use the
kqueuemonitor. Beware that on some systems the maximum number of file descriptors that can be opened by a process is set to a very low value (values as low as 256 are not uncommon), even if the operating system may allow a much larger value. In this case, check your OS documentation to raise this limit on either a per process or a system-wide basis. -
If feasible, watch directories instead of files. Properly crafting the receiving side of the events to deal with directories may sensibly reduce the monitor resource consumption.
-
On Windows, use the
windowsmonitor. -
If none of the above applies, use the poll monitor. The authors' experience indicates that
fswatchrequires approximately 150 MB of RAM memory to observe a hierarchy of 500.000 files with a minimum path length of 32 characters. A common bottleneck of the poll monitor is disk access, sincestat()-ing a great number of files may take a huge amount of time. In this case, the latency should be set to a sufficiently large value in order to reduce the performance degradation that may result from frequent disk access.
Getting fswatch
A regular user may be able to fetch fswatch from the package manager of your
OS or a third-party one. If you are looking for fswatch for macOS, you can
install it using either MacPorts or Homebrew:
# MacPorts
$ port install fswatch
# Homebrew
$ brew install fswatch
On FreeBSD, fswatch can be installed using pkg:
# pkg install fswatch-mon
Check your favourite package manager and let us know if fswatch is missing
there.
Building from Source
A user who wishes to build fswatch should get a release tarball.
A release tarball contains everything a user needs to build fswatch on their
system, following the instructions detailed in the Installation section below
and the INSTALL file.
A developer who wishes to modify fswatch should get the sources (either from a
source tarball or cloning the repository) and have the GNU Build System
installed on their machine. Please read README.gnu-build-system to get
further details about how to bootstrap fswatch from sources on your machine.
Getting a copy of the source repository is not recommended unless (i) you are a developer, (ii) you have the GNU Build System installed on your machine, and (iii) you know how to bootstrap it on the sources.
CMake-support is unofficially offered as a courtesy.
Installation
See the INSTALL file for detailed information about how to configure and
install fswatch. Since the fswatch builds and uses dynamic libraries, in
some platforms you may need to perform additional tasks before you can use
fswatch:
-
Make sure the installation directory of dynamic libraries (
$PREFIX/lib) is included in the lookup paths of the dynamic linker of your operating system. The default path,/usr/local/lib, will work in nearly every operating system. -
Refreshing the links and cache to the dynamic libraries may be required. In GNU/Linux systems you may need to run
ldconfig:$ ldconfig
fswatch is a C++ program and a C++ compiler compliant with the C++ standard
currently used is required.
As of version 1.17.1 included, fswatch has required a C++11-compliant
compiler. Subsequent versions require a C++17-compliant compiler. Check your
OS documentation for information about how to install the C++ toolchain and the
C++ runtime.
No other software packages or dependencies are required to configure and install
fswatch but the aforementioned APIs used by the file system monitors.
Documentation
fswatch provides the following documentation:
- Texinfo documentation, included with the distribution.
- HTML documentation.
- PDF documentation.
- A wiki page.
- A man page.
fswatch official documentation is provided in Texinfo format. This is the
most comprehensive source of information about fswatch and the only
authoritative one. The man page, in particular, is a stub that suggests the
user to use the info page instead.
If you are installing fswatch using a package manager and you would like the
PDF manual to be bundled into the package, please send a feature request to the
package maintainer.
Localization
fswatch is localizable and internally uses GNU gettext to decouple
localizable string from their translation. The currently available locales are:
- English (
en). - Italian (
it). - Spanish (
es).
To build fswatch with localization support, you need to have gettext
installed on your system. If configure cannot find <libintl.h> or the
linker cannot find libintl, then you may need to manually provide their
location to configure, usually using the CPPFLAGS and the LDFLAGS
variables. See README.macos for an example.
If gettext is not available on your system, fswatch shall build correctly,
but it will lack localization
Related Skills
node-connect
328.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
81.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
328.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
81.0kCommit, push, and open a PR
