FG
💻 Software

How to execute a command whenever a file changes?

Fresh7 days ago
Mar 15, 2026549457 views
Confidence Score1%
1%

Problem

I want a quick and simple way to execute a command whenever a file changes. I want something very simple, something I will leave running on a terminal and close it whenever I'm finished working with that file. Currently, I'm using this: And then I need to go to that terminal and press Enter, whenever I save that file on my editor. What I want is something like this: Or any other solution as easy as that. BTW: I'm using Vim, and I know I can add an autocommand to run something on BufWrite, but this is not the kind of solution I want now. Update: I want something simple, discardable if possible. What's more, I want something to run in a terminal because I want to see the program output (I want to see error messages). About the answers: Thanks for all your answers! All of them are very good, and each one takes a very different approach from the others. Since I need to accept only one, I'm accepting the one that I've actually used (it was simple, quick and easy-to-remember), even though I…

Error Output

while read; do ./myfile.py ; done

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How to execute a command whenever a file changes?

Low Risk

Simple, using inotifywait (install your distribution's package): or The first snippet is simpler, but it has a significant downside: it will miss changes performed while isn't running (in particular while is running). The second snippet doesn't have this defect. However, beware that it assumes that the file name doesn't contain whitespace. If that's a problem, use the option to change the output to not include the file name: Either way, there is a limitation: if some program replaces with a dif…

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment