In Linux, you may use cron to schedule commands to run at certain times. But what if you need commands to be run every time a file is modified, or a new file is added to a directory? That’s also easily achievable, and there are some command-line tools to perform that. Let’s get started!

watchexec

watchexec is a handy and standalone tool that monitors a given working directory and executes a command if it detects any changes like file updation or new file creation.

Features

It does not require a complex command line involving xargs (extended arguments).Checks for changes in the current directory and all subdirectories in real-time.There is no need for a language runtime, and it is not connected to any specific language or ecosystem..gitignore and .ignore are used by default to decide which files to ignore notifications for.Process groups are used to keep track of forking programs.Watching files with a certain extension is supported.It’s compatible with OS X, Linux, and Windows.Filtering and ignoring events based on glob (File-search patterns for finding partially identical filenames is possible.

Installation of watchexec

To easily install watchexec tool, paste the following link into the terminal or a shell prompt, and press enter. Linux & macOS After the successful installation, the path will be displayed on the screen. In my case, this tool was installed in /root/.local/bin And after installation, navigate to the .local/bin repository and check whether the file is there or not by using these commands. The next step is to export the path to use the watchexec tool. To run, Just type watchexec in the terminal as a root user. Use this command to see which flags and options can be used with watchexec tool.

Sample Example Commands

Call ls -la when any file changes in this directory/subdirectory. This command shows all the files present in the directory whenever it detects any modification.

Whenever any of the python, js, CSS, or Html extension files in the current directory change, run the command. Here you can pass any command you want. The file extensions should be separated by a comma.

Run command when any file in lib or src changes. “-w” option watches a specific file or directory in the system.

Call/restart any service when any file in the current directory (and all subdirectories) changes.

Here, “-r” option restarts the process or service if it’s running in the system. For more watchexec usage examples, you can visit the official GitHub repository.

entr

entr is a simple and excellent command-line utility for running arbitrary commands when any modifications occur in a given directory. “entr” stands for Event Notify Test Runner. This tool was created with the goal of making rapid feedback and automated testing.

Installation

entr is pre-installed in the Linux distribution. In case it’s missing, you need to install it manually. This tool is simple to use and can be installed with the following command. Or you can also install it by cloning the official Git repository. Next, navigate to that directory and install the requirements using the below commands. To see available build options run ./configure -h

Sample Example Commands

To see the options and arguments available for entr command. Use the following command. This command displays the user manual for the entr command.

To launch and auto-reload a MySQL server when any JavaScript file changes in the working directory. Each time the changes are saved to the file, entr reloads the MySQL server.

To Auto-reload a web server or terminate if the server exits.

For more details and examples on entr command, you can visit their official GitHub repository.

Final Words 👩‍💻

I hope you found this article very useful in learning how to run Linux commands when any file in a given directory changes and when new files are created. You may also be interested in learning how to remove files and directories in Linux.

How to Execute Command When File Changes in Linux  - 96How to Execute Command When File Changes in Linux  - 50How to Execute Command When File Changes in Linux  - 77How to Execute Command When File Changes in Linux  - 59How to Execute Command When File Changes in Linux  - 20How to Execute Command When File Changes in Linux  - 46How to Execute Command When File Changes in Linux  - 21How to Execute Command When File Changes in Linux  - 56How to Execute Command When File Changes in Linux  - 57How to Execute Command When File Changes in Linux  - 36How to Execute Command When File Changes in Linux  - 33How to Execute Command When File Changes in Linux  - 42How to Execute Command When File Changes in Linux  - 54How to Execute Command When File Changes in Linux  - 85How to Execute Command When File Changes in Linux  - 98How to Execute Command When File Changes in Linux  - 21How to Execute Command When File Changes in Linux  - 72How to Execute Command When File Changes in Linux  - 96How to Execute Command When File Changes in Linux  - 54How to Execute Command When File Changes in Linux  - 72How to Execute Command When File Changes in Linux  - 45How to Execute Command When File Changes in Linux  - 82How to Execute Command When File Changes in Linux  - 15