How to use the command 'entr' (with examples)

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 the make command whenever any file listed by ag -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 the make command followed by make test whenever any of the .c source files listed by ls *.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 a SIGTERM signal to any previously spawned Ruby subprocesses and then runs the ruby main.rb command when any of the .rb files listed by ls *.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 the psql command with the argument -f /_ when any of the .sql files listed by ls *.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.

Related Posts

How to use the command pnmscale (with examples)

How to use the command pnmscale (with examples)

The pnmscale command is a command-line tool that was used to scale images in the Portable Bitmap (PBM), Portable Graymap (PGM), and Portable Pixelmap (PPM) formats.

Read More
How to use the command `sum` (with examples)

How to use the command `sum` (with examples)

The sum command is used to compute checksums and the number of blocks for a file.

Read More
How to use the command "corebrightnessd" (with examples)

How to use the command "corebrightnessd" (with examples)

The “corebrightnessd” command is used to manage Night Shift, a feature available on macOS that helps reduce eye strain by adjusting the color temperature of the display to a warmer tone during the evening and night hours.

Read More