DEV Community

Michael Saparov
Michael Saparov

Posted on

entr: run commands when files change

entr is a tool I found by chance, and it pleasantly surprised me. It allows you to run commands when files change. I found very few articles about it, so I decided to write this note.
A basic usage example looks like this:

ls nginx.conf | entr nginx -t
Enter fullscreen mode Exit fullscreen mode

When the configuration file changes, entr runs nginx -t again. This is especially useful when editing configuration files, as it allows you to validate them immediately after saving.
Some flags make entr more comfortable to use.

Restart a long-running process (-r)

ls *.py | entr -r python my-server.py
Enter fullscreen mode Exit fullscreen mode

The -r flag restarts the command every time a file changes. This is important for long-running processes like development servers, because without -r the previous process would keep running.

Clear the screen before each run (-c)

ls *.conf | entr -c nginx -t
Enter fullscreen mode Exit fullscreen mode

The -c flag clears the terminal before running the command.

Watching new files (-p)

find . -name '*.conf' | entr -p nginx -t
Enter fullscreen mode Exit fullscreen mode

The -p flag also reacts to newly created files. This is useful when files are added dynamically.

Conclusion

If you often edit files and rerun commands manually, entr is a great tool.

Top comments (1)

Collapse
 
alexander_malyuk_0a532657 profile image
Alexander

Such a useful automation tool, thx