LinuxCommandLibrary

fswatch

Monitor file system changes and trigger actions

TLDR

Run a Bash command on file creation, update, or deletion

$ fswatch [path/to/file] | xargs [[-n|--max-args]] 1 [bash_command]
copy

Watch one or more files and/or directories
$ fswatch [path/to/file] [path/to/directory] [path/to/another_directory/**/*.js] | xargs [[-n|--max-args]] 1 [bash_command]
copy

Print the absolute paths of the changed files
$ fswatch [path/to/directory] | xargs [[-n|--max-args]] 1 -I _ echo _
copy

Filter by event type
$ fswatch --event [Updated|Removed|Created|...] [path/to/directory] | xargs [[-n|--max-args]] 1 [bash_command]
copy

SYNOPSIS

fswatch [options] [paths]

PARAMETERS

-h, --help
    Print usage information and exit.

-V, --version
    Print version and exit.

-v, --verbose
    Increase verbosity level.

-r, --recursive
    Watch directories recursively.

-o, --output FILE
    Append events to file instead of stdout.

-l, --latency TIMEOUT
    Set event coalescence latency in seconds (default 0.1).

-E, --event EVENT
    Print only specified event types (e.g., Updated, Created).

-t, --timestamp
    Print timestamp with each event.

-d, --display-monitor
    Print monitor name and inotify stats.

-x
    Print event names instead of codes.

--event-flags
    Print low-level event flags.

--format FORMAT
    Output format: default, csv, watchdog (JSON).

--no-defer
    Disable event queue deferral.

-i, --include REGEX
    Include paths matching regex.

-e, --exclude REGEX
    Exclude paths matching regex.

DESCRIPTION

fswatch is a command-line tool for watching file system events across directories. On Linux, it leverages the inotify API to detect changes like file creation, deletion, modification, and access. It outputs events in real-time, making it ideal for automating tasks such as rebuilding projects on file saves, running tests, or syncing files.

Unlike basic tools, fswatch supports recursive monitoring, event filtering, latency tuning for performance, and multiple output formats including CSV and Watchdog-style JSON. It's portable, working on Linux, macOS, BSD, and Windows. Events include attributes like path, event type (e.g., Created, Modified, Removed), and timestamps.

Common use: fswatch -r src/ | xargs -n1 make to rebuild on source changes. It's lightweight but requires installation via package managers like apt (libfswatch) or from source.

CAVEATS

Not installed by default; install via apt install fswatch or build from source. High directory counts may hit inotify limits (sysctl fs.inotify.max_user_watches). Recursive watch on large trees consumes resources.

EXAMPLE USAGE

fswatch -r -o changes.log .
Watches current directory recursively, logs events.

fswatch -E Updated,Deleted src/ | xargs make
Triggers make on updates/deletes.

INSTALLATION ON LINUX

Ubuntu/Debian: sudo apt install fswatch
Fedora: sudo dnf install fswatch
Or git clone https://github.com/emcrisostomo/fswatch.git && ./configure && make && sudo make install.

HISTORY

Developed by Enrico M. Crisostomo starting 2012. Initial Linux/macOS focus using inotify/FSEvents. v1.14+ added Windows support via ReadDirectoryChangesW. Actively maintained on GitHub with semantic versioning.

SEE ALSO

inotifywait(1), inotifywatch(1), entr(1), watch(1)

Copied to clipboard