How to Execute Command When File Changes in Linux?

You’ve come to the suitable position if you wish to have to find out how to run Linux instructions when any report in a operating listing alternate and when new recordsdata are created.

In Linux, you might use cron to time table instructions to run at sure instances.

But what if you wish to have instructions to be run each and every time a report is changed, or a brand new report is added to a listing?

That’s additionally simply achievable, and there are some command-line gear to carry out that.

Let’s get began!


watchexec is a to hand and standalone software that displays a given operating listing and executes a command if it detects any adjustments like report updation or new report advent.


  • It does now not require a posh command line involving xargs (prolonged arguments).
  • Checks for adjustments in the present listing and all subdirectories in real-time.
  • There is little need for a language runtime, and it isn’t attached to any particular language or ecosystem.
  • .gitignore and .forget about are utilized by default to make a decision which recordsdata to forget about notifications for.
  • Process teams are used to stay monitor of forking systems.
  • Watching recordsdata with a undeniable extension is supported.
  • It’s appropriate with OS X, Linux, and Windows.
  • Filtering and ignoring occasions in line with glob (File-search patterns for locating in part an identical filenames is imaginable.

Installation of watchexec

To simply set up watchexec software, paste the next hyperlink into the terminal or a shell steered, and press input.

Linux & macOS

curl -sS | bash

After the a hit set up, the trail shall be displayed at the display screen. In my case, this software was once put in in /root/.native/bin

└─# curl -sS | bash

Thanks for the usage of webi to set up '[email protected]' on 'Linux/x86_64'.
Have an issue? Experience a computer virus? Please tell us:

Lovin' it? Say thank you with a Star on GitHub:

Found /root/Downloads/webi/watchexec/cli-v1.18.9/watchexec-1.18.9-x86_64-unknown-linux-musl.tar.xz
Extracting /root/Downloads/webi/watchexec/cli-v1.18.9/watchexec-1.18.9-x86_64-unknown-linux-musl.tar.xz
Installing to /root/.native/decide/watchexec-vcli-v1.18.9/bin/watchexec
Installed 'watchexec vcli-v1.18.9' as /root/.native/bin/watchexec

And after set up, navigate to the .native/bin repository and test whether or not the report is there or now not through the usage of those instructions.

cd .native/bin


The subsequent step is to export the trail to use the watchexec software.

export PATH="/root/.native/bin:$PATH"

To run, Just kind watchexec in the terminal as a root consumer. Use this command to see which flags and choices can be utilized with watchexec software.

watchexec --help

Sample Example Commands

  • Call ls -la when any report adjustments in this listing/subdirectory. This command presentations all of the recordsdata provide in the listing each time it detects any amendment.
watchexec -- ls -la
  • Whenever any of the python, js, CSS, or Html extension recordsdata in the present listing alternate, run the command. Here you’ll be able to go any command you wish to have. The report extensions will have to be separated through a comma.
 watchexec --exts py,js,css,html <command>
  • Run command when any report in lib or src adjustments. “-w” choice watches a particular report or listing in the machine.
watchexec -w lib -w src <command>
  • Call/restart any provider when any report in the present listing (and all subdirectories) adjustments.
watchexec -e html -r tor

watchexec -e js,py -r mysql

Here, “-r” choice restarts the method or provider if it’s operating in the machine.

For extra watchexec utilization examples, you’ll be able to seek advice from the official GitHub repository.


entr is a straightforward and very good command-line software for operating arbitrary instructions when any changes happen in a given listing.

entr” stands for Event Notify Test Runner. This software was once created with the objective of creating speedy comments and automatic checking out.


entr is pre-installed in the Linux distribution. In case it’s lacking, you wish to have to set up it manually.

This software is inconspicuous to use and can also be put in with the next command.

sudo apt-get set up entr

Or you’ll be able to additionally set up it through cloning the professional Git repository.

git clone

Next, navigate to that listing and set up the necessities the usage of the beneath instructions.

make take a look at
make set up

To see to be had construct choices run ./configure -h

Sample Example Commands

To see the choices and arguments to be had for entr command. Use the next command.

guy entr

This command presentations the consumer handbook for the entr command.

     entr — run arbitrary instructions when recordsdata alternate

     entr [-acdnprsz] software [argument /_ ...]

     An inventory of recordsdata supplied on usual enter, and the software is done the usage of the provided arguments
     if any of them alternate.  entr waits for the kid procedure to end earlier than responding to next
     report machine occasions.  A TTY could also be opened earlier than getting into the watch loop in order to strengthen interac‐
     tive utilities.

     The arguments are as follows:

     -a      Respond to all occasions which happen whilst the software is operating.  Without this selection, entr
             consolidates occasions in order to steer clear of looping.  This choice has no impact in conjunction with
             the -r flag.

     -c      Clear the display screen earlier than invoking the software specified at the command line.  Specify two times to
             erase the scroll again buffer.

     -d      Track the directories of normal recordsdata supplied as enter and go out if a brand new report is added.
             This choice additionally permits directories to be specified explicitly.  If specified two times, all new
             entries to a listing are identified, differently recordsdata with names starting with ‘.’ are ig‐

     -n      Run in non-interactive mode.  In this mode entr does now not strive to learn from the TTY or
             alternate its houses.

     -p      Postpone the primary execution of the software till a report is changed.

 Manual web page entr(1) line 1 (press h for assist or q to give up)
  • To release and auto-reload a MySQL server when any JavaScript report adjustments in the operating listing. Each time the adjustments are stored to the report, entr reloads the MySQL server.
 ls *.js | entr -r mysql
  • To Auto-reload a internet server or terminate if the server exits.
$ ls * | entr -rz ./httpd

For extra main points and examples on entr command, you’ll be able to seek advice from their professional GitHub repository.

Final Words 👩‍💻

I am hoping you discovered this text very helpful in studying how to run Linux instructions when any report in a given listing adjustments and when new recordsdata are created.

You can be in studying how to take away recordsdata and directories in Linux.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button