How to use the command 'entr' (with examples)
The entr
command is a useful tool for running arbitrary commands when files change. It is especially handy for automating tasks in development workflows and continuous integration setups.
Use case 1: Rebuild with make
if any file in any subdirectory changes
Code:
ag -l | entr make
Motivation: When working on a large project with multiple subdirectories, it can be time-consuming to manually trigger a rebuild every time a file changes. This command solves that problem by automatically running make
whenever any file in any subdirectory changes.
Explanation:
ag -l
is a command that lists the filenames matching a given pattern. Here, it lists all files in the current directory and its subdirectories.entr make
runs themake
command whenever any file listed byag -l
changes.
Example output: If a file called file1.c
in the subdirectory src
is modified, make
will be automatically executed to rebuild the project.
Use case 2: Rebuild and test with make
if any .c
source files in the current directory change
Code:
ls *.c | entr 'make && make test'
Motivation: During development, it’s important to continuously test code changes to ensure they don’t introduce any regressions. This command makes it easy to run both the build and test steps whenever a .c
source file is modified.
Explanation:
ls *.c
lists all.c
source files in the current directory.entr 'make && make test'
runs themake
command followed bymake test
whenever any of the.c
source files listed byls *.c
change.
Example output: If file2.c
in the current directory is modified, make
and make test
will be executed subsequently.
Use case 3: Send a SIGTERM
to any previously spawned ruby subprocesses before executing ruby main.rb
Code:
ls *.rb | entr -r ruby main.rb
Motivation: When writing complex Ruby scripts, it is common to spawn subprocesses that may not terminate themselves. This command provides a clean way to automatically terminate any previously spawned Ruby subprocesses before running the ruby main.rb
command.
Explanation:
ls *.rb
lists all.rb
files in the current directory.entr -r ruby main.rb
sends aSIGTERM
signal to any previously spawned Ruby subprocesses and then runs theruby main.rb
command when any of the.rb
files listed byls *.rb
change.
Example output: If helper.rb
is modified, any previously spawned Ruby subprocesses will be terminated and ruby main.rb
will be executed.
Use case 4: Run a command with the changed file (/_
) as an argument
Code:
ls *.sql | entr psql -f /_
Motivation: When working with databases, it is common to have SQL files that contain schema or data changes. This command allows running these SQL files automatically whenever they are modified, passing the changed file as an argument to the psql
command.
Explanation:
ls *.sql
lists all.sql
files in the current directory.entr psql -f /_
runs thepsql
command with the argument-f /_
when any of the.sql
files listed byls *.sql
change. Here,/_
is a placeholder that will be replaced by the path of the changed file.
Example output: If schema.sql
is modified, the psql
command will be executed as psql -f schema.sql
.
Conclusion:
The entr
command is a powerful tool that simplifies automation of tasks based on file changes. By using this tool, developers can save time and effort by automating repetitive tasks in their workflows. Whether it’s rebuilding, testing, terminating processes, or reacting to changed files, entr
provides a flexible and efficient solution.