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] path ...

PARAMETERS

-0, --null
    Use null character for separating file names in output.

-c, --config
    Read configuration from the specified file.

-e, --event
    Add an event filter.

-f, --format
    Specify output format (e.g., 'standard', 'json', 'plain').

-h, --help
    Display help and exit.

-i, --include
    Add an include filter.

-l, --latency
    Set the latency in seconds.

-n, --no-recursive
    Disable recursive monitoring.

-p, --pid-file
    Write process ID to file.

-r, --recursive
    Enable recursive monitoring.

-v, --version
    Display version information and exit.

path ...
    Paths to be monitored. At least one path is required.

DESCRIPTION

fswatch is a cross-platform program that monitors a file system for directory content modifications. It can recursively watch directories and notify you when files or directories are modified.

Unlike some other tools, fswatch provides various event filters and fine-grained control over how changes are reported. It's highly configurable, allowing you to specify which events trigger notifications (e.g., file creation, deletion, modification), filter events based on regular expressions, and execute custom commands when changes occur. This makes it a powerful tool for automated tasks like rebuilding projects, synchronizing files, or triggering alerts in response to filesystem events.

CAVEATS

fswatch relies on operating system-specific APIs for monitoring, so its behavior might vary slightly across platforms. It is resource intensive when watching a very large amount of files.

Also be aware of file system event coalescing. Some fast changes could be missed.

EXAMPLES

fswatch .: Monitors the current directory recursively.
fswatch -o . | xargs -n 1 -I {} echo 'File changed: {}': Monitors the current directory and prints the name of each changed file.
fswatch -r -e Created -e Updated -e Removed /path/to/directory: Monitors '/path/to/directory' recursively and only reports 'Created', 'Updated', and 'Removed' events.

SEE ALSO

Copied to clipboard