obda.net

Automatically Run Commands with inotifywait

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

You can use inotifywait from inotify-tools to automatically run commands, for example whenever a file is written to.

The following shell loop runs xelatex whenever ~/input.tex changes:

inotifywait -m -e close_write ~/input.tex | while read line
do
    xelatex -interaction nonstopmode ~/input.tex
done

It is also possible to watch more than one file. Additionally, if using the -r switch, any specified directory will be watched recursively:

inotifywait -m -e close_write -r locale/ | while read line
do
    python manage.py compilemessages
done

Update: December 19, 2015

The previous version of this article did not make use of inotifywait’s -m option:

while inotifywait -e close_write -r locale/
do
    python manage.py compilemessages
done

This way, inotify’s watches are re-setup after each single run of the specified command. If watching large directory trees, this is a pretty expensive operation.

In contrast, the -m option activates inotifywait’s “monitor” mode—the necessary watches are set up only once, and inotifywait runs indefinitely.

Convert Filenames Recursively to Lower Case

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

Use zmv to recursively convert the names of all files and (sub-) directories within the current directory to lower case:

autoload zmv
zmv '(**/)(*)' '$1${2:l}'

If you want to convert to upper case instead of lower case, use :u instead of :l.

Default File Permissions for a Directory

Add a comment

cheat-sheet articles are about code snippets that I need every once in a while, and which I constantly forget about.

You can set the default permissions for all files created in a given directory using POSIX access control lists (ACLs). For example, after executing

user@host:~$ setfacl -d -m u::rwx,g::rwx,o::r-x ~/path/to/example/dir

all new files (and directories, too) in ~/path/to/example/dir will have group write permission enabled. To check the currently effective ACLs, use getfacl.