Linux provides a nice interface for monitoring all file system events like creating, modifying, removing files. The interface is
inotify family of system calls, the userspace utilities leveraging these calls are provided by the
inotify-tools package in Ubuntu (available on the universe repository). If you don't have it already, install by:
sudo apt-get install inotify-tools
inotifywatch binaries, we need the first one.
So you want to run the command
asciidoctor -q some_file when any
.adoc file is modified (
some_file will replaced by that), if so assuming your
.adoc files are in directory
/foo/bar, you can set the watch:
inotifywait -qm --event modify --format '%w' /foo/bar/*.adoc
-q enables the quiet mode, no info from
-m enables monitor mode, otherwise it will exit after the first event
--event modify, we are only interested in
modify event i.e. when a file is modified. Other possible values include
--format %w, we only want the file name that is modified rather than bunch of other info as we will use the file name as input to another command
/foo/bar/*.adoc will be expanded to all
.adoc files under
Now the above will show you the filename whenever any is modified, now to run the command on the filename (assuming the command takes arguments via STDIN):
inotifywait -qm --event modify --format '%w' /foo/bar/*.adoc | asciidoctor -q
You can also setup a recursive watch on the directory, you will then need to use
grep to filter the desired files only. Here setting the watch recursively (
-r) on directory
/foo/bar and using
grep to filter only
inotifywait -qrm --event modify --format '%w%f' /foo/bar | grep '\.adoc$' | asciidoctor -q
When watching directories the output format specifier
%w resolves to the directory name, so we need
%f to get the file name. While watching files,
%f would resolve to empty string.
Note that, you can also run
inotifywait in daemon (
-d) mode, you can also script the whole thing, and/or run in background, and/or play with it more other options.
Also, you can replace
asciidoctor with any other command of your choice, if you want.
man inotifywait to get more idea.