File change event linux

Command to monitor file changes within a directory and execute command

to compile it into HTML format. I am trying to automate the process. so far I have come with this using entr:

ls *.adoc | entr asciidoctor -q *.adoc 

but only works with existing folder not for subfolders. I have tried this variation, but it doesn’t work:

find . -name '*.adoc' | entr asciidoctor -q *.adoc 

4 Answers 4

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 

inotify-tools provides inotifywait and 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 inotifywait itself
  • -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 open , close etc.
  • —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 /foo/bar directory

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 | while read -r file ; do asciidoctor -q "$file" done 

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 .adoc files:

inotifywait -qrm --event modify --format '%w%f' /foo/bar | grep '\.adoc$' | while read -r file ; do asciidoctor -q "$file" done 

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.

Читайте также:  Linux файл конфигурации пользователя

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.

Check man inotifywait to get more idea.

Источник

How to run a shell script when a file or directory changes?

@MerlynMorgan-Graham I’d move this to superuser, because the answer might not have anything do with programming — i.e. there might be some program or configuration option that can be used, without any programming needed. I had the same question, and searched superuser first :p

12 Answers 12

You may try entr tool to run arbitrary commands when files change. Example for files:

$ ls -d * | entr sh -c 'make && make test' 
$ ls *.css *.html | entr reload-browser Firefox 

or print Changed! when file file.txt is saved:

$ echo file.txt | entr echo Changed! 

For directories use -d , but you’ve to use it in the loop, e.g.:

while true; do find path/ | entr -d echo Changed; done 
while true; do ls path/* | entr -pd echo Changed; done 

entr is the simplest, most composable and unix-y tool for the job. Love it. incron can be replaced with entr and a process manager like runit , s6 or even systemd .

I have a script regularly appending to a log file. When I use entr to monitor that log file and touch the log, everything works fine, but when the script appends to the file, entr fails. This may be because I have noatime set in my fstab for my ssd — but that only stops the updating of the access time not the modify time, so this confuses me. I have then tried entr -cdr on the directory of files that are updated with the log. That recognizes with the directory contents change, but the -r does not work. The entr process just ends.

Читайте также:  Изменить права доступа всей папки linux

Источник

Shell command to monitor changes in a file

I know there was a command on Unix that I could use to monitor a file and see changes that are getting written to it. This was quite useful especially for checking log files. Do you know what it is called?

14 Answers 14

Sidenote: If your distribution provides the tailf command, use that in preference to tail -f. tailf is more efficient because it doesn’t need to access the watched file if it’s not being written to (poll accesses are annoying if you mounted the file system with atime updating.)

tail -F will follow filenames rather than file objects, which is especially useful in case of log file rotation.

Update, a few years later: tailf is now deprecated and tail -f is safe. (confirm this on your system with man tailf .) See documentation: man7.org/linux/man-pages/man1/tailf.1.html

You probably meant tail, as per Jon Skeet’s answer.

Another useful one is watch; it allows you to run a command periodically and see the output full screen. For example:

watch -n 10 -d ls -l /var/adm/messages

Will run the command ls -l /var/adm/messages every 10 seconds, and highlight the difference in the output between subsequent runs. (Useful for watching how quickly a logfile is growing, for example).

inotifywait from inotify-tools is useful if you want to run a command every time a file (or any files in a directory) change. For example:

inotifywait -r -m -e modify /var/log | while read file_path file_event file_name; do echo $$ event: $ done 
Setting up watches. Beware: since -r was given, this may take a while! Watches established. /var/log/messages event: MODIFY /var/log/kern event: MODIFY . 

Just a note that path isn’t the greatest choice for a variable name. On zsh , it seems that environment vars aren’t case-sensitive. For me, setting path causes PATH to also get set, and that basically means nothing will execute until you fix that. On bash , setting path has no effect on PATH .

Читайте также:  Путь до утилиты linux

@Thanatos Zsh variables are case-sensitive, but among the variables set by Zsh itself, Zsh «ties» the *PATH variables to an array of the same name, but lowercase. Tied variables always consist of a scalar and an array (e.g. PATH and path ), and modifying one modifies the other. A key feature is that the array version is automatically split on the separator in the scalar version (the : ). See for yourself with print «$PATH\n$path» . The second paragraph in the PARAMETERS USED BY THE SHELL section in the zshparam(1) man page has more detailed information.

As a further note, there’s quite a few variables used by Zsh that are tied, not just PATH and path . They are all listed in the section in my previous comment including, but not limited to: FPATH / fpath , CDPATH / cdpath , MANPATH / manpath , FIGNORE / fignore , and more.

I prefer using less +FG 1 over tail -f because I find myself needing to search a log file for a specific error or ID. If I need to search for something, I type ^C to stop following the file and ? to start searching backwards.

Key bindings are pretty much the same as in vi . Any command can be initialized on startup using the + option:

+cmd Causes the specified cmd to be executed each time a new file is examined. For example, +G causes less to initially display each file starting at the end rather than the beginning. 

For really long logs, I find it convenient to use the -n option which turns off line numbering. From the manpage:

-n or --line-numbers Suppresses line numbers. The default (to use line numbers) may cause less to run more slowly in some cases, especially with a very large input file. Suppressing line numbers with the -n option will avoid this problem. Using line numbers means: the line number will be displayed in the verbose prompt and in the = command, and the v command will pass the current line number to the editor (see also the discussion of LESSEDIT in PROMPTS below). 

1. Hat-tip to rgmarcha for pointing this out in the comments.

Источник

Оцените статью
Adblock
detector