Execute a Command when Files Change

· ? cm

I recently added another monitor to my setup and wanted to re-execute a Python script whenever I modified it for data analysis. That way, I don’t have to manually switch windows and issue the command.

A fantastic tool for that purpose is watchexec: Its syntax is very intuitive and it works across platforms.

Now, watching for changed files, clearing the terminal and executing a command is just one invocation away:

watchexec -e py -c -r python main.py

Another similar program is entr, which you can invoke as follows:

ls *.py | entr -rc python main.py

I prefer watchexec because it comes with good defaults, such as automatically respecting your .gitignore file.