Table of Contents
Usage
Start ./onfilechange.sh -h
to get the help
______________________________________________________________________________
T R I G G E R C O M M A N D O N A F I L E C H A N G E
_______________________________________________________________________| v1.06
HELP:
onfilechange.sh checks the change of a given fileobjects and triggers
a command on file changes.
It can use stat or inotifywait to watch a file change. Default is stat because
it can be used for non existing files.
SYNTAX:
onfilechange.sh OPTIONS [FILE] [[... FILE N]]
PRAMETERS:
-c|--command COMMAND
A command to execute on a file change
-f|--file FILENAME
Filenames or directories to watch; separate multiple files with
space and put all in quotes
DEPRECATED: add all files to watch as parameters
-h|--help
show this help
-i|--inotifywait
force inotifywait command
-s|--stat
force stat command (default mode)
-v|--verbose
verbose mode; enable showing debug output; it should be the first option
to see handling of other options
-w|--wait SLEEPTIME
for stat mode: wait time in seconds betweeen each test or on
missing file; default: 5 sec
EXAMPLES:
onfilechange.sh -c "ls -l" /home/me/touchfile.txt
watch touchfile.txt and make a file listing on change
onfilechange.sh -c "ls -l" "/home/me/touchfile.txt" "home/me/touchfile2.txt"
watch touchfile.txt and touchfile2.txt
onfilechange.sh -s -w 10 -c "echo hello" /home/me/touchfile.txt
watch touchfile.txt every 10 sec with stat and show "hello" on a change
Test on command line
To make a test on commandline touch a testfile touch /tmp/mytestfile
.
The start the script with enabled debugging (-d) in a 2nd terminal:
/usr/local/bin/onfilechange.sh -c 'echo Changes!'
“/tmp/mytestfile“
______________________________________________________________________________
T R I G G E R C O M M A N D O N A F I L E C H A N G E
_______________________________________________________________________| v1.06
--- summary
checking file(s): /tmp/mytestfile
with command [stat]
with a sleep time of [5] seconds
on change execute command [echo Changes!]
...............................................................................
>>>>> watched files
ls: cannot access '/tmp/mytestfile': No such file or directory
INFO: file '/tmp/mytestfile' (or one of them) does not exist yet
>>>>> start
waiting for file changes ...
If you create the file in another terminal window touch /tmp/mytestfile
then you get the reaction:
>>>>> start
waiting for file changes ...
1c1
< stat: cannot statx '/tmp/mytestfile': No such file or directory
---
> regular empty file /tmp/mytestfile | perms: -rw-r--r--; user 1000 (axel) group 1000 (axel) | size: 0 byte | last modification 2025-01-17 13:39:02.543806368 +0100
>>>>> Fri Jan 17 13:39:06 CET 2025 Executing echo Changes! ...
The given command will be executed and its output is visible. Then it waits again.
Changes!
Command was successful.
>>>>> waiting for the next change ...
Tipps and tricks
Multiple files
You cannot watch just a single file but multiple files too. Just add all of them at the and of the command line as parameters.
onfilechange.sh -c <command> <FILE_1> <FILE_2> <FILE:N>
The files are seperated by spaces. If they contain spaces or other special chars you need to quote them or mask the special characters. Both of these examples are equal:
onfilechange.sh -c <command> "directory 1" "directory 2"
onfilechange.sh -c <command> directory\ 1 directory\ 2
Run permanently
If the file watcher should run always ans always start after reboot, then create a service. In the chapter Service is an example for systemd.