entr: Run arbitrary commands when files change

The “entr” command-line tool is designed to run arbitrary commands whenever files change in a specified directory. It provides a convenient way to automate tasks or processes that need to be triggered based on file modifications. With “entr,” you can easily monitor files and execute custom commands in response to changes, making it a powerful tool for automating workflows and development tasks.

Here’s a more detailed explanation of the “entr” command-line tool and its key features:

  • File Monitoring: The primary function of “entr” is to monitor files in a given directory for changes. It continuously watches the specified files and directories for modifications, such as file creation, modification, deletion, or attribute changes.
  • Triggering Custom Commands: When a change is detected in any of the monitored files, “entr” triggers the execution of a custom command or a script specified by the user. This allows you to define and run any command or series of commands in response to file changes.
  • Flexible Command Execution: “entr” supports executing a wide range of commands, including shell commands, scripts, build processes, test runners, and more. You can define complex commands with options, arguments, and redirections to perform specific actions based on file changes.
  • Fast and Efficient: “entr” is designed to be lightweight and efficient. It uses system events and file monitoring mechanisms to detect changes quickly, ensuring that your custom commands are executed promptly when files are modified.
  • Cross-Platform Compatibility: “entr” is compatible with various operating systems, including Linux, macOS, BSD, and other Unix-like systems. This makes it a versatile tool that can be used across different environments.
  • Integration with Other Tools: “entr” can be easily integrated into existing workflows and development tools. For example, you can combine it with build systems, test runners, code linters, or task runners to automate specific actions whenever relevant files are modified.
  • Continuous Testing and Development: “entr” is particularly useful for continuous testing and development scenarios. It allows you to automatically trigger tests, rebuild projects, reload applications, or perform any other necessary actions whenever files are modified. This can significantly speed up the development cycle and streamline iterative processes.

Recursive Monitoring: “entr” supports recursive monitoring, allowing you to monitor an entire directory tree and its subdirectories. This is helpful when you want to track changes in multiple levels of nested directories or when working on projects with complex directory structures.

Simple Usage and Workflow Integration: “entr” follows a simple command-line interface, making it easy to incorporate into scripts, build processes, or interactive development sessions. You can use it in combination with other command-line tools or in shell pipelines to create complex workflows.

“entr” provides a flexible and efficient way to run custom commands or scripts whenever files change. By monitoring file modifications and triggering actions accordingly, it helps automate repetitive tasks, streamline development workflows, and improve productivity.

entr Command Examples

1. Rebuild with make if any file in any subdirectory changes:

# ag -l | entr make

2. Rebuild and test with make if any .c source files in the current directory change:

# ls *.c | entr 'make && make test'

3. Send a SIGTERM to any previously spawned ruby subprocesses before executing ruby main.rb:

# ls *.rb | entr -r ruby main.rb

4. Run a command with the changed file (/_) as an argument:

# ls *.sql | entr psql -f /_
Related Post