10

When I write a LaTeX document, I find myself to press F6 in my editor to compile often. Or when I use SASS, I often type make to convert my .scss into .css.

Now I would like to let the machine do the dirty work and run make or something else every time a given file change.

So far I kept a terminal window open and pressed arrow-up and return. Or I used watch make, but that seems like an odd solution.

Is there a nice way on Linux to do something when a given file changes?

3 Answers 3

4

You're looking for inotifywait, part of inotify-tools. There are some examples of how to use it on the project's site but a simple approach is,

while true; do inotifywait code.cpp -e modify; make; done

the key part of which is,

inotifywait code.cpp -e modify

That command will wait until code.cpp is modified then exit. Put in the infinite loop and followed with make causes it to constantly rebuild after every modification.

Besides waiting on modify, you can wait on creation, deletion, access, opening, closing, and more. I highly recommend looking at the project examples and inotifywait man page for some other ideas on how it can be used.

1
  • That is exactly what I am looking for! May 1, 2012 at 9:52
1

Have a look at incron, a cron-like package that responds to filesystem events, instead of time events. The package consists of a daemon (incrond) and a table manipulator (incrontab), analogous to the well-known crond/crontab pair of cron.

With incron, you could easily arrange to run make whenever particular files and/or directories are modified.

1
  • That seems a little overkill on the one hand, but on the other it is just the right tool. I'll have to check it out, maybe I can enable and disable it for each programming session. Apr 27, 2012 at 17:18
0

Rather than watch make, how about watch ls? Then just make when the timestamp changes.

3
  • Well, make already checks the timestamps, so it is rather pointless to check the time and then let make check the time again. Apr 26, 2012 at 7:27
  • Right but running make is more expensive than running ls. Apr 26, 2012 at 10:06
  • 1
    And if my makefile is just two lines for that single .tex? Do you think that would be worth writing an ls parser? Apr 27, 2012 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .