How to Use the entr Command in Linux to Automatically Run Commands on File Changes

During software development, testing, and script writing, repeating the same manual cycle—modifying a source file, switching to the terminal, and re-running a test or compiler command—wastes valuable focus and momentum. While complex build tools provide integrated file watchers, they frequently require hefty language dependencies and extensive configuration files. The entr (Event Notify Test Runner) utility is a lightweight, Unix-philosophical command-line tool that monitors arbitrary file lists and automatically triggers commands whenever those files are modified on disk.

Why Use entr in the Linux Terminal?

Unlike heavy background daemons, entr operates directly via standard input pipes (stdin), adhering to core Unix principles:

  • Zero Configuration: Feeds from standard utilities like find, ls, or git ls-files without requiring custom YAML or JSON configs.
  • Resource Efficient: Leverages the Linux kernel’s native inotify file system notification interface rather than polling disk sectors, keeping CPU usage virtually at 0%.
  • Process Restarts: Features a persistent server mode (-r) that gracefully terminates and restarts background services (e.g., Python web servers or Node.js APIs) on file save.
  • Terminal Clearing: Offers clean screen resets (-c) before each run, ensuring you only view the newest compilation errors or test outputs.

Installing entr on Linux Distributions

The entr binary is lightweight and available across all major distribution repositories:

Ubuntu, Debian, and Linux Mint

sudo apt update
sudo apt install entr

Arch Linux and Manjaro

sudo pacman -S entr

Fedora, CentOS Stream, and Rocky Linux

sudo dnf install entr

Basic Syntax and How entr Works

Because entr reads file targets from standard input, you pipe a list of file paths into it, followed by the command you want to execute:

find . -name "*.py" | entr pytest

In this workflow, find streams the paths of all Python files in the current directory into entr. The moment you save an edit to any of those files inside your code editor, entr detects the modification event and executes pytest immediately.

Practical Example 1: Running Unit Tests with a Clear Screen

When running unit tests or linters, having old error traces linger in the terminal window can be confusing. Supply the -c flag to clear the terminal buffer before each execution:

git ls-files | entr -c npm test

Using git ls-files is an excellent best practice: it monitors all project files tracked by Git while ignoring untracked build artifacts, virtual environments, and node_modules folders.

Practical Example 2: Automatically Restarting a Web Server or Daemon

If you are writing an API or backend service (such as a Flask, FastAPI, or Express app), you need the process to stay alive and restart seamlessly whenever code changes. Use the -r (restart) flag:

find . -name "*.go" | entr -r go run main.go

When an edit occurs, entr sends a SIGTERM signal to the running go run process, waits for it to close cleanly, and spawns a fresh instance with your updated code.

Practical Example 3: Recompiling C/C++ or Markdown Documentation

If you only want your command to run against the specific file that changed rather than rebuilding the entire project, use the /_ placeholder:

find docs/ -name "*.md" | entr pandoc /_ -o output.pdf

Here, /_ dynamically expands to the exact filename that triggered the inotify event, compiling only the altered document.

Watching Newly Created Files with the -d Flag

By default, entr watches the static list of files piped to it at startup. If you create a brand-new file inside a directory, entr will not detect it unless you pass the -d flag to watch directory containers:

while true; do
    find . -type f -name "*.rs" | entr -d cargo check
done

Wrapping entr -d inside a simple shell loop causes entr to exit cleanly when a new file appears, allowing the outer loop to immediately re-evaluate find and watch the updated file collection.

The entr command provides an indispensable, zero-overhead productivity tool that brings instant feedback loops to any Linux terminal workflow.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.