entr: The Standalone File Watcher

August 2, 2023

What is entr?

entr is just a file watcher. The README says:

Run arbitrary commands when files change

It’s available for most package managers: brew install entr

A simple example

List the files you’re interested in, one per line:

> ls *.txt

Now feed them to entr with the command you want to run:

> ls *.txt | entr make

Why entr?

A lot of projects and languages have their own “watcher” or “–watch” flag.

But I enjoy tools that work with everything that I want to do.

Even if you feel that webpack (for example) is fulfilling all your needs, would you use it to watch a few .txt files and trigger some custom job?

I don’t want every tool to roll out their own “watcher”. That’s not the Unix philosophy.

Detour: how to list files, one per line

Usually, you would feed the output of ls or find to entr.

There are occasions where that list will be too large. Or, you might want a custom list of files. How do you print arguments one per line?

> printf "%s\n" a.txt b.txt c.txt

(see: Why is printf better than echo?)

Other options I’ve used:

# output of "grep"
> rg -l query

# current in-play git files
> git status --short | sed 's/^...//'
# or some other git-generated list

# specific source file and its test file
> printf "%s\n" lib/whatever.ex test/whatever.exs

Noteworthy tips

The source of truth is the man page. Here are a few tips:

I also found that combining entr with ding allows me to run “headless”

> printf "%s\n" lib/whatever.ex test/whatever.exs | entr ding mix test

I can hear when tests pass/fail based on the exit code. 😎

Discuss on Twitter