Debian Codegraph @

man/fswatch.7.in

raw
1 .\" $Id$
2 .\" Copyright (c) 2013-2026 Enrico M. Crisostomo <[email protected]>
3 .\"
4 .\" This program is free software; you can redistribute it and/or modify
5 .\" it under the terms of the GNU General Public License as published by
6 .\" the Free Software Foundation; either version 3, or (at your option)
7 .\" any later version.
8 .\"
9 .\" This program is distributed in the hope that it will be useful,
10 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 .\" GNU General Public License for more details.
13 .\"
14 .\" You should have received a copy of the GNU General Public License
15 .\" along with this program. If not, see <http://www.gnu.org/licenses/>.
16 .\"
17 .\" Man page for the @FSWATCH@ command.
18 .\"
19 .\" $Log$
20 .\"
21 .Dd @MAN_DATE@
22 .Dt "@FSWATCH@" "7" "@FSWATCH@ file system monitor"
23 .Os "@TARGET_VENDOR@" "@TARGET_OS@"
24 .Pp
25 .Sh NAME
26 .Nm @FSWATCH@
27 .Nd Ask for notification when the contents of the specified files or directory
28 hierarchies are modified.
29 .Pp
30 This man page is kept for reference but it is not to be considered an
31 authoritative or complete source of information. Please, consult the official
32 Info documentation shipped with
33 .Nm .
34 .Sh SYNOPSIS
35 .Nm @FSWATCH@ (option)* path+
36 .Sh DESCRIPTION
37 The
38 .Nm
39 command receives notifications when the contents of the specified files or
40 directories are modified. @FSWATCH@ implements seven kind of monitors:
41 .Bl -tag -width indent
42 .It -
43 A monitor based on the File System Events API of Apple macOS.
44 .It -
45 A monitor based on kqueue, an event notification interface introduces in FreeBSD
46 4.1 and supported on most *BSD systems (including macOS).
47 .It -
48 A monitor based on the File Events Notification API of the Solaris kernel and
49 its derivatives.
50 .It -
51 A monitor based on inotify, a Linux kernel subsystem that reports file system
52 changes to applications.
53 .It -
54 A monitor based on fanotify, a Linux kernel subsystem that can report file
55 system changes with optional process attribution.
56 .It -
57 A monitor based on the ReadDirectoryChangesW Microsoft Windows API.
58 Native MinGW-w64 builds are the recommended Windows target; Cygwin builds are
59 kept for compatibility when the Cygwin path conversion API is available.
60 .It -
61 A monitor which periodically stats the file system, saves file modification
62 times in memory and manually calculates changes.
63 .El
64 .Pp
65 .Nm
66 writes a record for each event it receives containing:
67 .Bl -tag -width indent
68 .It -
69 The timestamp when the event was
70 .Em received
71 (optionally).
72 .It -
73 The path affected by the current event.
74 .It -
75 A space-separated list of
76 .Em event types
77 (see
78 .Sx EVENT TYPES
79 ).
80 .El
81 .Pp
82 By default,
83 .Nm
84 enters an infinite loop and never returns. When it receives a SIGABRT, SIGINT
85 or SIGTERM signal,
86 .Nm
87 closes the notification streams and exits gracefully returning 0 to the calling
88 process. (Unless running in
89 .Op Fl 1, -one-event
90 mode, in which case it sets its return code according to the signal it
91 received, as usual.)
92 .Pp
93 The following options are available:
94 .Bl -tag -width indent
95 .It Fl 0, -print0
96 Use the ASCII NUL character (\e0) as line separator. Since file names can
97 potentially contain any character but NUL, this option assures that the output
98 of @FSWATCH@ can be safely parsed using NUL as delimiter, such as using xargs -0
99 and the shell builtin read -d ''.
100 .It Fl 1, -one-event
101 Exit fswatch after the first set of events is received.
102 .It Fl -batch-marker Ar marker
103 Print a marker at the end of every batch. An optional marker
104 .Ar marker
105 can be specified to override its default value `NoOp'.
106 .It Fl -event Ar name
107 Filter event with the specified
108 .Ar name .
109 This option can be used multiple times, one for each event
110 .Ar name
111 that must be included in the output.
112 .It Fl e, -exclude Ar regexp
113 Exclude paths matching
114 .Ar regexp .
115 Multiple exclude filters can be specified using this option multiple times. See
116 .Sx FILTERING PATHS
117 for further information.
118 .It Fl E, -extended
119 Use extended regular expressions.
120 .It Fl -filter-mode Ar mode
121 Set the path filter evaluation mode.
122 .Ar mode
123 can be
124 .Ql legacy
125 or
126 .Ql conjunctive .
127 See
128 .Sx FILTERING PATHS
129 for further information.
130 .It Fl f, -format-time Ar format
131 Print the event time using the specified
132 .Ar format .
133 Supported formats are specified by
134 .Xr strftime 3 .
135 .It Fl -format Ar format
136 Use the specified event record format. The
137 .Ar format
138 string supports path, flag, timestamp, correlation, and monitor-specific
139 metadata directives including fanotify process attribution.
140 .It Fl h, -help
141 Show the help message.
142 .It Fl i, -include Ar regexp
143 Include paths matching
144 .Ar regexp .
145 Multiple include filters can be specified using this option multiple times.
146 See
147 .Sx FILTERING PATHS
148 for further information.
149 .It Fl I, -insensitive
150 Use case insensitive regular expressions.
151 .It Fl l, -latency Ar latency
152 Set the
153 .Ar latency
154 in seconds.
155 The latency is a double value greater than 0.1.
156 Smaller values are currently not allowed in order not to compromise the
157 performance of the system.
158 The default latency is 1 second.
159 .It Fl L, -follow-links
160 Follow symbolic links.
161 .It Fl M, -list-monitors
162 List the available monitors.
163 .It Fl m, -monitor Ar name
164 Uses the monitor specified by
165 .Ar name .
166 The list of currently available monitors can be obtained using the
167 .Fl h
168 option.
169 .It Fl -no-defer
170 Sets the kFSEventStreamCreateFlagNoDefer flag on macOS FSEvents monitor, which
171 makes the monitor more responsive. This flag is more appropriate for
172 sessions, while the default behaviour is more appropriate for background,
173 daemon or batch processing apps.
174 .It Fl n, -numeric
175 Print the numeric value of the event flag, instead of the array of symbolic
176 names. The numeric value of the event flags are system-specific and may vary
177 across different versions of macOS. As a consequence, the use of numeric values
178 is discouraged.
179 .It Fl o, -one-per-batch
180 Print a single message with the number of change events.
181 .It Fl -prune Ar regexp
182 Do not descend into directories matching
183 .Ar regexp
184 during recursive traversal. This option may be specified multiple times. See
185 .Sx FILTERING PATHS
186 for further information.
187 .It Fl r, -recursive
188 Watch subdirectories recursively. This option may not be supported on all
189 systems.
190 .It Fl t, -timestamp
191 Print the event timestamp.
192 .It Fl u, -utc-time
193 Print the event time in UTC format. When this option is not specified, the time
194 is printed using the system
195 .Em local
196 time, as defined by
197 .Sy localtime (3) .
198 .It Fl v, -verbose
199 Print verbose output.
200 .It Fl -version
201 Print the version of
202 .Nm
203 and exits.
204 .It Fl x, -event-flags
205 Prints the event flags.
206 .It Fl -event-flag-separator Ar separator
207 Print event flags using the specified
208 .Ar separator .
209 .El
210 .Sh MONITORS
211 .Nm
212 acts as a front-end to system-specific monitors. Currently, the available
213 monitors are:
214 .Bl -tag -width indent
215 .It -
216 The
217 .Em FSEvents monitor ,
218 a monitor based on the File System Events API of Apple macOS.
219 .It -
220 The
221 .Em kqueue monitor ,
222 a monitor based on kqueue, an event notification interface introduced in FreeBSD
223 4.1 and supported on most *BSD systems (including macOS).
224 .It -
225 The
226 .Em inotify monitor ,
227 a Linux kernel subsystem that reports file system changes to applications.
228 .It -
229 The
230 .Em fanotify monitor ,
231 a Linux kernel subsystem that can report file system changes with optional
232 process attribution.
233 .It -
234 The
235 .Em poll monitor ,
236 a monitor which periodically stats the file system, saves file modification
237 times in memory and manually calculates file system changes, which can work on
238 any operating system where
239 .Xr stat 2
240 can be used.
241 .El
242 .Pp
243 Each monitor has its own strengths, weakness and peculiarities. Although
244 .Nm
245 strives to provide a uniform experience no matter which monitor is used, it is
246 still important for users to know which monitor they are using and to be aware
247 of existing bugs, limitations, corner cases or pathological behaviour.
248 .Ss The FSEvents Monitor
249 The
250 .Em FSEvents monitor ,
251 available only on Apple macOS, has no known limitations and scales very well with
252 the number of files being observed. In fact, I observed no performance
253 degradation when testing
254 .Nm
255 observing changes on a filesystem of 500 GB over long periods of time. On macOS,
256 this is the default monitor.
257 .Ss The kqueue Monitor
258 The
259 .Em kqueue monitor ,
260 available on any *BSD system featuring kqueue, requires a file descriptor to be
261 opened for every file being watched. As a result, this monitor scales badly
262 with the number of files being observed and may begin to misbehave as soon as
263 the
264 .Nm
265 process runs out of file descriptors. In this case,
266 .Nm
267 dumps one error on standard error for every file that cannot be opened. Beware
268 that on some systems the maximum number of file descriptors that can be opened
269 by a process is set to a very low value (values as low as 256 are not uncommon),
270 even if the operating system may allow a much larger value.
271 .Pp
272 If you are running out of file descriptors when using this monitor and you
273 cannot reduce the number of observed items, either:
274 .Bl -tag -width indent
275 .It -
276 Consider raising the number of maximum open file descriptors (check your OS
277 documentation).
278 .It -
279 Consider using another monitor.
280 .El
281 .Ss The inotify Monitor
282 The
283 .Em inotify monitor ,
284 available on Linux since kernel 2.6.13, may suffer a queue overflow if events
285 are generated faster than they are read from the queue. In any case, the
286 application is guaranteed to receive an overflow notification which can be
287 handled to gracefully recover.
288 .Nm
289 currently throws an exception if a queue overflow occurs. Future versions will
290 handle the overflow by emitting proper notifications. However, the odds of
291 observing a queue overflow on a default configured mainstream GNU/Linux
292 distribution is very low.
293 .Pp
294 The inotify API provides no information about the user or process that
295 triggered an event, so applications cannot easily distinguish events they
296 trigger themselves from events triggered by other processes. Inotify reports
297 only events triggered through the filesystem API, so it does not catch remote
298 events that occur on network filesystems. Furthermore, pseudo-filesystems such
299 as \fI/proc\fR, \fI/sys\fR, and \fI/dev/pts\fR are not monitorable with
300 inotify.
301 .Pp
302 The inotify API does not report file accesses or modifications that may occur
303 because of \fBmmap(2)\fR, \fBmsync(2)\fR, and \fBmunmap(2)\fR. It identifies
304 affected files by filename, but by the time the application processes the
305 event, the filename may already have been deleted or renamed. Events are
306 identified via watch descriptors, so applications must cache a mapping, if one
307 is needed, between watch descriptors and pathnames. Be aware that directory
308 renamings may affect multiple cached pathnames.
309 .Pp
310 The inotify API sends events for the direct child elements of a watched
311 directory and it scales pretty well with the number of watched items. For this
312 reason, depending on the number of files to watch, it may sometimes be
313 preferable to watch a common parent directory and filter received events rather
314 than adding a huge number of file watches. Inotify monitoring of directories
315 is not recursive, so additional watches must be created for subdirectories.
316 That can take a significant amount of time for large directory trees.
317 .Pp
318 If monitoring an entire directory subtree, and a new subdirectory is created in
319 that tree or an existing directory is renamed into that tree, be aware that by
320 the time you create a watch for the new subdirectory, new files and
321 subdirectories may already exist inside the subdirectory. Therefore, you may
322 want to scan the contents of the subdirectory immediately after adding the
323 watch, and, if desired, recursively add watches for any subdirectories that it
324 contains.
325 .Pp
326 The event queue can overflow. In this case, events are lost. Robust
327 applications should handle the possibility of lost events gracefully. For
328 example, it may be necessary to rebuild part or all of the application cache.
329 .Pp
330 If a filesystem is mounted on top of a monitored directory, no event is
331 generated, and no events are generated for objects immediately under the new
332 mount point. If the filesystem is subsequently unmounted, events will
333 subsequently be generated for the directory and the objects it contains.
334 .Ss The fanotify Monitor
335 The
336 .Em fanotify monitor ,
337 available on Linux when the build system detects the required fanotify headers
338 and symbols, uses file-handle based fanotify events. It is not the default
339 Linux monitor; select it explicitly with:
340 .Pp
341 .Dl "$ @FSWATCH@ -m fanotify_monitor path ..."
342 .Pp
343 The fanotify monitor is useful when Linux-specific process attribution is
344 required. Custom format directive
345 .Em %K
346 prints the identifier kind, such as `pid' or `tid', and
347 .Em %P
348 prints the reported process or thread identifier. The monitor property
349 .Em fanotify.process-id
350 accepts `pid' or `tid'. The
351 .Em fanotify.report-pidfd
352 property requests pidfd reporting when supported by the build headers and
353 kernel; the pidfd is exposed through the library event metadata only while the
354 callback is executing.
355 .Pp
356 Fanotify reports only events triggered through the filesystem API, so it does
357 not catch remote events that occur on network filesystems. It also does not
358 report accesses or modifications that may occur because of \fBmmap(2)\fR,
359 \fBmsync(2)\fR, and \fBmunmap(2)\fR. Directory events are created only if the
360 directory itself is opened, read, and closed, so adding, removing, or
361 changing children of a marked directory does not create events for the
362 monitored directory itself. Fanotify monitoring of directories is not
363 recursive: monitoring subdirectories requires additional marks, and using
364 \fBFAN_CREATE\fR to detect them is racy because events may be lost before a new
365 mark is added. Monitoring mounts or entire filesystems avoids that race. The
366 event queue can also overflow, in which case events are lost.
367 .Pp
368 The
369 .Em fanotify.unlimited-queue
370 and
371 .Em fanotify.unlimited-marks
372 properties request
373 .Em FAN_UNLIMITED_QUEUE
374 and
375 .Em FAN_UNLIMITED_MARKS ,
376 respectively. These properties are opt-in because they require matching build
377 headers and the capability expected by the running kernel, typically
378 .Em CAP_SYS_ADMIN ,
379 and fanotify initialization fails if the process lacks it.
380 .Pp
381 Fanotify support depends on kernel, C library, filesystem, and permission
382 support for the requested mode. Some filesystems do not support file handles,
383 and some fanotify modes require additional privileges. Use the inotify monitor
384 when broad Linux compatibility is more important than process attribution.
385 .Pp
386 Fanotify directory marks are not equivalent to macOS FSEvents recursive tree
387 watches. With ordinary directory marks,
388 .Em FAN_EVENT_ON_CHILD
389 reports events for immediate children only, not grandchildren or deeper
390 descendants. Recursive fanotify monitoring therefore still marks
391 subdirectories explicitly. Avoiding recursive mark expansion requires
392 fanotify mount or filesystem marks, which have broader scope and different
393 permission constraints.
394 .Ss The Poll Monitor
395 The
396 .Em poll monitor
397 was added as a fallback mechanisms in the cases where no other monitor could be
398 used, including:
399 .Bl -tag -width indent
400 .It -
401 Operating system without any sort of file events API.
402 .It -
403 Situations where the limitations of the available monitors cannot be overcome
404 (i.e.: observing a number of files greater than the available file descriptors
405 on a system using the kqueue monitor).
406 .El
407 .Pp
408 The poll monitor, available on any platform, only relies on available CPU and
409 memory to perform its task (besides the
410 .Xr stat 2
411 function). The performance of this monitor degrades linearly with the number of
412 files being watched. The authors' experience indicates that
413 .Nm
414 requires approximately 150 MB or RAM memory to observe a hierarchy of 500.000
415 files with a minimum path length of 32 characters. A common bottleneck of the
416 poll monitor is disk access, since stat()-ing a great number of files may take a
417 huge amount of time. In this case, the latency should be set to a sufficiently
418 large value in order to reduce the performance degradation that may result from
419 frequent disk access.
420 .Ss How to Choose a Monitor
421 .Nm
422 already chooses the "best" monitor for your platform if you do not specify any.
423 However, a specific monitor may be better suited to specific use cases. Please,
424 read the
425 .Sx MONITORS
426 section to get a description of all the available monitors and their
427 limitations.
428 .Pp
429 Usage recommendations are as follows:
430 .Bl -tag -width indent
431 .It -
432 On macOS, use only the FSEvents monitor (which is the default behaviour).
433 .It -
434 On Linux, use the inotify monitor (which is the default behaviour).
435 .It -
436 On Linux, use the fanotify monitor only when its Linux-specific process
437 attribution or fanotify semantics are required.
438 .It -
439 If the number of files to observe is sufficiently small, use the kqueue monitor.
440 Beware that on some systems the maximum number of file descriptors that can be
441 opened by a process is set to a very low value (values as low as 256 are not
442 uncommon), even if the operating system may allow a much larger value. In this
443 case, check your OS documentation to raise this limit on either a per process or
444 a system-wide basis.
445 .It -
446 If feasible, watch directories instead of watching files. Properly crafting the
447 receiving side of the events to deal with directories may sensibly reduce the
448 monitor resource consumption.
449 .It -
450 If none of the above applies, use the poll monitor. The authors' experience
451 indicates that fswatch requires approximately 150 MB or RAM memory to observe a
452 hierarchy of 500.000 files with a minimum path length of 32 characters. A
453 common bottleneck of the poll monitor is disk access, since stat()-ing a great
454 number of files may take a huge amount of time. In this case, the latency
455 should be set to a sufficiently large value in order to reduce the performance
456 degradation that may result from frequent disk access.
457 .El
458 .Sh FILTERING PATHS
459 Received events can be filtered by path using regular expressions. Regular
460 expressions can be used to include or exclude matching paths.
461 .Pp
462 Explicit command-line root paths remain eligible for monitor setup even when an
463 output filter would reject the root path itself.
464 .Nm
465 supports two path filter evaluation modes:
466 .Ql legacy
467 and
468 .Ql conjunctive .
469 .Pp
470 The default
471 .Ql legacy
472 mode preserves historical behavior. The user can specify multiple filter
473 expressions in any order and filters are processed this way:
474 .Bl -tag -width indent
475 .It -
476 If a path matches an including filter, the path is accepted no matter
477 any other filter.
478 .It -
479 If a path matches an excluding filter, the path is rejected.
480 .It -
481 If a path matches no filters, the path is accepted.
482 .El
483 Said another way:
484 .Bl -tag -width indent
485 .It -
486 All paths are accepted
487 .Em by default ,
488 unless an exclusion filter
489 says otherwise.
490 .It -
491 Inclusion filters may override any other exclusion filter.
492 .It -
493 The order in the definition of filters in the command line has no effect.
494 .El
495 .Pp
496 The
497 .Ql conjunctive
498 mode is enabled with
499 .Fl -filter-mode=conjunctive .
500 It treats inclusion filters as the candidate set and exclusion filters as
501 subtractions from that set:
502 .Bl -tag -width indent
503 .It -
504 If no inclusion filters are specified, all paths are candidates.
505 .It -
506 If at least one inclusion filter is specified, a path must match an inclusion
507 filter to be a candidate.
508 .It -
509 Any matching exclusion filter rejects the path.
510 .El
511 .Pp
512 Said another way, conjunctive mode accepts paths matching this expression:
513 .Pp
514 .Dl (no includes OR matches include) AND NOT matches exclude
515 .Pp
516 For example, the following command reports changes to C source files except
517 editor lock files:
518 .Pp
519 .Dl $ fswatch --filter-mode=conjunctive -i '\e.c$' -e '/lock-[^/]+\.c$' .
520
521 .Ss Pruning Recursive Traversal
522 The
523 .Fl -prune Ar regexp
524 option defines regular expressions that stop recursive traversal from
525 descending into matching directories. This is different from
526 .Fl -exclude :
527 excluded paths are filtered from reported events, while pruned directories
528 and their descendants are skipped during library-side recursive traversal.
529 .Pp
530 Pruning only affects monitors that perform this traversal in
531 .Nm .
532 Native recursive backends may accept
533 .Fl -prune ,
534 but may not be able to prevent the operating system from traversing matching
535 subtrees in the same way.
536 .Pp
537 Prune expressions use the same case-sensitivity and extended-regular-expression
538 settings as normal path filters.
539 .Pp
540 Pruning is applied only to non-root directories. A command-line root path
541 remains eligible for monitoring even if it matches a prune expression.
542 .Pp
543 For example, the following command recursively watches
544 .Pa /home/me
545 without descending into hidden directories:
546 .Pp
547 .Dl $ fswatch -r --prune '/\e.[^/]+$' /home/me
548
549 .Pp
550 Other options govern how regular expressions are interpreted:
551 .Bl -tag -width indent
552 .It -
553 Regular expressions can be
554 .Em extended
555 if option
556 .Fl E
557 is specified.
558 .It -
559 Regular expressions can be
560 .Em case insensitive
561 if option
562 .Fl I
563 is specified.
564 .El
565 .Sh EVENT TYPES
566 Event flags identify the kind of change a file system object has undergone. Many of them directly map to common file system operations (such as creation, deletion, update, etc.), others are less common (such as attribute modification), and others are monitor and platform specific.
567 .sp
568 Currently,
569 .Nm
570 maps monitor-specific event flags to 'global' event flags acting as a sort of 'greatest common denominator' of all the available monitor flags. The following global event flags are available::
571 .Bl -tag -width 20n
572 .It Sy NoOp
573 Idle event, optionally issued when no changes were detected.
574 .It Sy PlatformSpecific
575 This event maps a platform-specific event that has no corresponding flag.
576 .It Sy Created
577 The object has been created.
578 .It Sy Updated
579 The object has been updated. The kind of update is monitor-dependent.
580 .It Sy Removed
581 The object has been removed.
582 .It Sy Renamed
583 The object has been renamed.
584 .It Sy OwnerModified
585 The object’s owner has changed.
586 .It Sy AttributeModified
587 An object’s attribute has changed.
588 .It Sy MovedFrom
589 The object has moved from this location to a new location of the same file system.
590 .It Sy MovedTo
591 The object has moved from another location in the same file system into this location.
592 .It Sy IsFile
593 The object is a regular file.
594 .It Sy IsDir
595 The object is a directory.
596 .It Sy IsSymLink
597 The object is a symbolic link.
598 .It Sy Link
599 The object link count has changed.
600 .It Sy Overflow
601 The monitor has overflowed.
602 .El
603 .Sh EXAMPLES
604 .Ss Basic Usage
605 .Nm
606 syntax is the following:
607 .Pp
608 .Dl $ fswatch [options] [paths] ...
609 .Pp
610 .Nm
611 will then output change events to standard output. By default, only the affected
612 file name is printed. However, many options are available to format the event
613 record, including:
614 .Bl -tag -width indent
615 .It -
616 The possibility of adding the event timestamp.
617 .It -
618 The possibility of adding the event mask in both textual and numerical form.
619 .El
620 .Pp
621 The following command listens for changes in the current directory and events
622 are delivered every 5 seconds:
623 .Pp
624 .Dl "$ @FSWATCH@ -l 5 ."
625 .Pp
626 The following command listens for changes in the current user home directory and
627 .Em /var/log :
628 .Pp
629 .Dl "$ @FSWATCH@ ~ /var/log"
630 .Ss Piping @FSWATCH@ Output to Another Process
631 Very often you wish to not only receive an event, but react to it. The simplest
632 way to do it is piping fswatch output to another process. Since in Unix and
633 Unix-like file system file names may potentially contain any character but
634 .Em NUL (\e0)
635 and the path separator
636 .Em (/) ,
637 .Nm
638 has a specific mode of operation when its output must be piped to another
639 process. When the
640 .Op Fl 0
641 option is used,
642 .Nm
643 will use the
644 .Em NUL
645 character as record separator, thus allowing any other character to appear in a
646 path. This is important because many commands and shell builtins (such as
647 .Em read )
648 split words and lines by default using the characters in
649 .Em $IFS ,
650 which by default contains characters which may be present (although rarely) in a
651 file name, resulting in a wrong event path being received and processed.
652 .Pp
653 Probably the simplest way to pipe
654 .Nm
655 to another program in order to respond to an event is using
656 .Em xargs :
657 .Pp
658 .Dl "$ fswatch -0 [opts] [paths] | xargs -0 -n 1 -I {} [command]"
659 .Pp In this example:
660 .Bl -tag -width indent
661 .It -
662 .Em fswatch -0
663 will split records using the
664 .Em NUL
665 character.
666 .It -
667 .Em xargs -0
668 will split records using the
669 .Em NUL
670 character. This is required to correctly match impedance with
671 .Nm .
672 .It -
673 .Em xargs -n 1
674 will invoke
675 .Em command
676 every record.
677 If you want to do it every
678 .Em x
679 records, then use
680 .Em xargs -n x .
681 .It -
682 .Em xargs -I {}
683 will substitute occurrences of
684 .Em {}
685 in command with the parsed argument. If the command you are running does not
686 need the event path name, just delete this option. If you prefer using another
687 replacement string, substitute
688 .Em {}
689 with yours.
690 .El
691 .Pp
692 On BSD-derived systems, including macOS, the system
693 .Em xargs
694 implementation may limit the size of replacement strings used by
695 .Fl I .
696 If commands fail when processing long path names, consider using a different
697 consumer that reads
698 .Nm Ns 's
699 NUL-separated records directly, or install GNU findutils and use
700 .Em gxargs
701 instead of
702 .Em xargs .
703 .Ss Bubbling Events
704 An often requested feature is being able to receive a single event "per batch",
705 instead of receiving multiple events. This use case is implemented by the
706 .Op Fl o, -one-per-batch
707 option which tells
708 .Nm
709 to dump a record containing the number of received events, without any other
710 detail:
711 .Pp
712 .Dl $ fswatch -or /path/to/watch
713 .Dl 1
714 .Dl 10
715 .Dl [...]
716 .Pp
717 This is useful if, for example, you want to respond to change events in a way
718 which is (or can easily be) path-independent (because you are not receiving any
719 event detail) and you prefer to "bubble" events together to reduce the overhead
720 of the command being executed. A typical case is a directory synchronisation
721 job whenever some files change.
722 .Ss Receiving a Single Event
723 Another requested feature is the possibility of receiving a single event and
724 exit. This is most useful when existing scripts processing events include the
725 restart logic of
726 .Nm
727 This use case is implemented by the
728 .Op Fl 1, -one-event
729 option:
730 .Pp
731 .Dl $ fswatch -1 /path/to/watch
732 .Dl /path/to/watch
733 .Sh Compatibility With @FSWATCH@ 0.x
734 The previous major version of
735 .Nm
736 (v. 0.x) allowed users to run a command whenever a set of changes was detected
737 with the following syntax:
738 .Pp
739 .Dl $ fswatch path program
740 .Pp
741 Starting with
742 .Nm
743 v. 1.x this behaviour is no longer supported. The rationale behind this
744 decision includes:
745 .Bl -tag -width indent
746 .It -
747 The old version only allows watching one path.
748 .It -
749 The command to execute was passed as last argument, alongside the path to watch,
750 making it difficult to extend the program functionality to add multiple path
751 support
752 .It -
753 The old version forks and executes /bin/bash, which is neither portable, nor
754 guaranteed to succeed, nor desirable by users of other shells.
755 .It -
756 No information about the change events is passed to the forked process.
757 .El
758 .Pp
759 To solve the aforementioned issues and keep
760 .Nm
761 consistent with common UNIX practices, the behaviour has changed and
762 .Nm
763 now prints event records to the standard output that users can process further
764 by piping the output of
765 .Nm
766 to other programs.
767 .Pp
768 To fully support the old use, the
769 .Op Fl o, -one-per-batch
770 option was added in v. 1.3.3.
771 When specified,
772 .Nm
773 will only dump 1 event to standard output which can be used to trigger another
774 program:
775 .Pp
776 .Dl $ fswatch -o path | xargs -n1 program
777 .Pp
778 In this case, program will receive the number of change events as first
779 argument. If no argument should be passed to program, then the following
780 command could be used:
781 .Pp
782 .Dl $ fswatch -o path | xargs -n1 -I{} program
783 .Sh EXIT STATUS
784 .Nm
785 may exit with one of the following exit statuses:
786 .Pp
787 .Dl FSW_OK 0
788 .Dl FSW_ERR_UNKNOWN_ERROR (1 << 0)
789 .Dl FSW_ERR_SESSION_UNKNOWN (1 << 1)
790 .Dl FSW_ERR_MONITOR_ALREADY_EXISTS (1 << 2)
791 .Dl FSW_ERR_MEMORY (1 << 3)
792 .Dl FSW_ERR_UNKNOWN_MONITOR_TYPE (1 << 4)
793 .Dl FSW_ERR_CALLBACK_NOT_SET (1 << 5)
794 .Dl FSW_ERR_PATHS_NOT_SET (1 << 6)
795 .Dl FSW_ERR_UNKNOWN_MONITOR (1 << 7)
796 .Dl FSW_ERR_MISSING_CONTEXT (1 << 8)
797 .Dl FSW_ERR_INVALID_PATH (1 << 9)
798 .Dl FSW_ERR_INVALID_CALLBACK (1 << 10)
799 .Dl FSW_ERR_INVALID_LATENCY (1 << 11)
800 .Dl FSW_ERR_INVALID_REGEX (1 << 12)
801 .Dl FSW_ERR_MONITOR_ALREADY_RUNNING (1 << 13)
802 .Dl FSW_ERR_STALE_MONITOR_THREAD (1 << 14)
803 .Dl FSW_ERR_THREAD_FAULT (1 << 15)
804 .Dl FSW_ERR_UNSUPPORTED_OPERATION (1 << 16)
805 .Dl FSW_ERR_UNKNOWN_VALUE (1 << 17)
806 .Sh DIAGNOSTICS
807 .Nm
808 exits 0 on success, and >0 if an error occurs.
809 .Sh COMPATIBILITY
810 .Nm
811 can be built on any system supporting at least one of the available monitors.
812 .Sh BUGS
813 See https://github.com/emcrisostomo/fswatch/issues
814 for open issues or to create a new one.
815 .Pp
816 Bugs can also be submitted to @MAN_BUG_REPORT@.