Posted on
Software

entr: Re-run commands when files change

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering entr: Automate Command Execution upon File Changes in Linux

In the world of software development, or any field that involves editing files and needing immediate feedback on changes, manually rerunning commands can be a hassle. Fortunately, entr exists to streamline this process. Short for Event Notify Test Runner, entr is a simple yet powerful Unix utility that runs arbitrary commands when files change. It’s highly useful for tasks like auto-compiling code, refreshing applications, or running tests in real-time. In this blog post, we'll explore how to install entr, use it effectively in your workflows, and highlight some practical examples.

Installing entr

Ubuntu and Debian-based Distributions:

For those using Debian, Ubuntu, or any derivatives, entr can be installed using the apt package manager. Here’s how to do it:

  1. First, update your package list to ensure you’re grabbing the latest version:

    sudo apt update
    
  2. Install entr:

    sudo apt install entr -y
    

Fedora, Red Hat, and CentOS:

If you're on a distribution that uses dnf, such as Fedora, you can install entr easily using the following commands:

  1. Update your DNF package repository (if needed):

    sudo dnf check-update
    
  2. Install entr:

    sudo dnf install entr
    

openSUSE:

For openSUSE users, the zypper package manager is the tool of choice. You can install entr using zypper by running:

  1. Refresh your repository index:

    sudo zypper refresh
    
  2. Install entr:

    sudo zypper install entr
    

How Does entr Work?

entr watches a list of specified files and runs a specified command whenever any of these files change. This can greatly speed up the development and testing process by automating routine tasks.

Getting Started with entr

To use entr, you simply need to provide a list of files to watch and a command to execute when changes are detected. This is typically done by piping a list of files to entr. Here’s a basic syntax:

ls *.py | entr sh -c "your-command-here"

In this example, entr watches all Python files (*.py) in the current directory. Whenever any Python file changes, entr will execute the command specified inside the quotes.

Practical Examples

Here are a few ways you can use entr in your projects:

1. Automatically Compile a Program

If you’re working on a C project, you can have entr recompile your code whenever a source file changes:

ls *.c | entr sh -c "gcc -o myprogram *.c && ./myprogram"

2. Refresh Your Webpage Automatically

Web developers can use entr to refresh a webpage automatically whenever HTML or CSS files are modified:

ls *.html *.css | entr sh -c "refresh-browser-command"

3. Run Unit Tests Automatically

For a Python project, you can rerun tests whenever any source file changes:

ls *.py | entr sh -c "pytest"

Tips for Using entr More Effectively

  • Use the -r flag: This flag tells entr to restart a persistent utility, such as a development server, instead of just rerunning the command.

  • Combine with find: For more complex projects, you might want to watch files in subdirectories or follow specific patterns, you can combine entr with find:

    find . -name '*.py' | entr sh -c "your-command-here"
    
  • Watch the output: If your commands generate important output, make sure to keep an eye on it to detect errors or important warnings.

entr is straightforward but incredibly effective for automating reactions to file changes. To build more complex automations, consider combining entr with scripts or other command-line tools to tailor it precisely to your workflow. With entr, you can make your development process more efficient and responsive, saving valuable time and reducing repetitive tasks.