How to Execute Command When File Changes in Linux?

You’ve got come to the proper place if you wish to discover ways to run Linux instructions when a file modifications in a workbook and when new recordsdata are created.

Below Linux you possibly can cron to schedule instructions to run at particular occasions.

However what if it’s essential to run instructions each time a file is modified or a brand new file is added to a folder?

That is additionally straightforward to perform, and there are some command line instruments to do this.

Let’s begin!

watch exec

watchexec is a helpful and standalone software that displays a specific workbook and executes a command if it detects modifications, similar to updating recordsdata or creating new recordsdata.


  • It doesn’t require a fancy command line xargs (in depth arguments).
  • Checks for modifications within the present folder and all subfolders in actual time.
  • No language runtime is required and it isn’t tied to any particular language or ecosystem.
  • ignore .git And .to disregard are utilized by default to determine which recordsdata to disregard notifications for.
  • Course of teams are used to maintain monitor of forking packages.
  • Viewing recordsdata with a sure extension is supported.
  • It’s suitable with OS X, Linux and Home windows.
  • Filtering and ignoring occasions based mostly on glob (file search patterns to seek out partially an identical file names is feasible.

Set up of watchexec

Simple to put in watchexec software, paste the next hyperlink into the terminal or a shell immediate and hit enter.

Linux and macOS

curl -sS | bash

After the profitable set up, the trail might be displayed on the display. In my case, this software was put in /root/.native/bin

└─# curl -sS | bash

Thanks for utilizing webi to put in 'watchexec@steady' on 'Linux/x86_64'.
Have an issue? Expertise a bug? Please tell us:

Lovin' it? Say thanks with a Star on GitHub:

Discovered /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
Putting in to /root/.native/decide/watchexec-vcli-v1.18.9/bin/watchexec
Put in 'watchexec vcli-v1.18.9' as /root/.native/bin/watchexec

And after set up, navigate to it .native/bin repository and verify whether or not the file is there or not utilizing these instructions.

cd .native/bin


The subsequent step is to export the trail to it watch exec software.

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

To run, simply kind watchexec within the terminal as root person. Use this command to see which flags and choices can be utilized with the watchexec software.

watchexec --help

Pattern pattern instructions

  • Cellphone name ls -la when a file on this folder/subfolder modifications. This command lists all recordsdata within the folder when modifications are detected.
watchexec -- ls -la
  • When any of the python, js, css, or html extension recordsdata within the present listing modifications, run the command. Right here you possibly can cross any command you need. The file extensions should be separated by a comma.
 watchexec --exts py,js,css,html <command>
  • Run the command when a file is current lib or src modifications. The “-w” choice displays a particular file or listing within the system.
watchexec -w lib -w src <command>
  • Name/restart any service when a file within the present listing (and all subdirectories) modifications.
watchexec -e html -r tor

watchexec -e js,py -r mysql

Right here the “-r” choice restarts the method or service whether it is working within the system.

For extra watchexec use circumstances, you possibly can go to the official GitHub repository.


entr is a straightforward and glorious command line software for working arbitrary instructions when modifications are made in a specific listing.

entrance” means Occasion message Check runR. This software was created with the purpose of offering fast suggestions and automatic testing.

Set up

entr is pre-installed within the Linux distribution. Whether it is lacking, you should set up it manually.

This software is straightforward to make use of and might be put in with the next command.

sudo apt-get set up entr

Or you may as well set up it by cloning the official Git repository.

git clone

Then navigate to that folder and set up the conditions utilizing the instructions under.

make check
make set up

To see the accessible construct choices, run ./configure -h

Pattern pattern instructions

To see the accessible choices and arguments entr command. Use the next command.

man entr

This command opens the person information for the entr command.

     entr — run arbitrary instructions when recordsdata change

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

     A listing of recordsdata offered on commonplace enter, and the utility is executed utilizing the provided arguments
     if any of them change.  entr waits for the kid course of to complete earlier than responding to subsequent
     file system occasions.  A TTY can also be opened earlier than getting into the watch loop in an effort to help interac‐
     tive utilities.

     The arguments are as follows:

     -a      Reply to all occasions which happen whereas the utility is working.  With out this feature, entr
             consolidates occasions in an effort to keep away from looping.  This feature has no impact along with
             the -r flag.

     -c      Clear the display earlier than invoking the utility specified on the command line.  Specify twice to
             erase the scroll again buffer.

     -d      Monitor the directories of normal recordsdata offered as enter and exit if a brand new file is added.
             This feature additionally allows directories to be specified explicitly.  If specified twice, all new
             entries to a listing are acknowledged, in any other case recordsdata with names starting with ‘.’ are ig‐

     -n      Run in non-interactive mode.  On this mode entr doesn't try and learn from the TTY or
             change its properties.

     -p      Postpone the primary execution of the utility till a file is modified.

 Guide web page entr(1) line 1 (press h for assist or q to stop)
  • To start out and robotically reload a MySQL server when a JavaScript file modifications within the working listing. Every time the modifications are saved to the file, entr reloads the MySQL server.
 ls *.js | entr -r mysql
  • To robotically reload or terminate an online server when the server shuts down.
$ ls * | entr -rz ./httpd

For extra particulars and examples entr command, you possibly can go to their official GitHub repository.

Final phrases 👩‍💻

I hope you discovered this text very useful in studying tips on how to run Linux instructions when a file in a specific listing modifications and when new recordsdata are created.

You may additionally be focused on studying tips on how to delete recordsdata and folders in Linux.

Rate this post
porno izle altyazılı porno porno