Running Tasks When Files Change

Learn about entr command and its various uses.

We'll cover the following

Executing commands with entr

The entr command can watch files for changes and execute any command we need. There are plenty of other utilities available, but entr is one of the most flexible options, and it requires no configuration to use. To install entr using the package manager, we run this:

sudo apt install entr

The entr command takes a list of files from STDIN and executes the given command when the files change. Let’s use entr to watch a Markdown document for changes and use pandoc to convert it to an HTML file.

First, let’s create a Markdown document to watch called watchme.md:

## Hello world
This is a paragraph
### This is a a heading
This is [a link](http://google.com). 
### This is a third heading

The entr command accepts a list of files as input. The easiest way to provide that input is to pipe the results of an ls command to entr. After that, we can tell entr what to do with those files. Let’s execute the following command to tell entr to create the file watchme.html from the watchme.md file any time watchme.md changes:

$ ls watchme.md | entr pandoc -t html -f markdown -o watchme.html /_

Get hands-on with 1200+ tech skills courses.