I want to use the inotify
mechanism on Linux. I want my application to know when a file aaa
was changed. Can you please provide me with a sample how to do that?

- 10,129
- 5
- 47
- 71

- 1,099
- 2
- 10
- 11
-
2Your trusted source of information should(and must) be the [man pages](http://man7.org/linux/man-pages/man7/inotify.7.html). It contains a lucid example, all one could ever ask for! If you would like to have a full blown project reference, give [fluffy](https://github.com/six-k/fluffy/blob/master/libfluffy/fluffy.h) interface a look. – six-k Mar 18 '18 at 16:50
4 Answers
- Documentation ( from Monitor file system activity with inotify )
The inotify
C API
inotify
provides three system calls to build file system monitors of all kinds:
inotify_init()
creates an instance of theinotify
subsystem in the kernel and returns a file descriptor on success and-1
on failure. Like other system calls, ifinotify_init()
fails, checkerrno
for diagnostics.inotify_add_watch()
, as its name implies, adds a watch. Each watch must provide a pathname and a list of pertinent events, where each event is specified by a constant, such asIN_MODIFY
. To monitor more than one event, simply use the logical or — the pipe (|) operator in C—between each event. Ifinotify_add_watch()
succeeds, the call returns a unique identifier for the registered watch; otherwise, it returns-1
. Use the identifier to alter or remove the associated watch.inotify_rm_watch()
removes a watch.
The read()
and close()
system calls are also needed. Given the descriptor yielded by inotify_init()
, call read()
to wait for alerts. Assuming a typical file descriptor, the application blocks pending the receipt of events, which are expressed as data in the stream. The common close() on the file descriptor yielded from inotify_init()
deletes and frees all active watches as well as all memory associated with the inotify instance. (The typical reference count caveat applies here, too. All file descriptors associated with an instance must be closed before the memory consumed by the watches and by inotify is freed.)
- An example ( from Kernel Korner - Intro to inotify )
#include "inotify.h" #include "inotify-syscalls.h" int wd; wd = inotify_add_watch (fd, "/home/rlove/Desktop", IN_MODIFY | IN_CREATE | IN_DELETE); if (wd < 0) perror ("inotify_add_watch");
This example adds a watch on the directory /home/rlove/Desktop for any modifications, file creations or file deletions.

- 10,129
- 5
- 47
- 71

- 12,746
- 4
- 44
- 50
-
Is there a particular reason why both you and Fabian chose not to give examples for shell scripting of inotify? – puk Nov 11 '11 at 19:12
-
4inotify itself is a Linux kernel API and not a user-space program you could use in a shell script. If you want to use the inotify API in your scripts, take a look at inotify-tools (https://github.com/rvoicilas/inotify-tools/wiki/). – joschi Nov 12 '11 at 15:02
-
I just use inotifywait/watch for my directories (via shell scripts). Is there something wrong with these two? – puk Nov 12 '11 at 21:55
-
3Please read my comment again. `inotifywait` is part of the mentioned inotify-tools. Additionally the original question was about a Qt application and not a shell script. – joschi Nov 13 '11 at 10:18
-
47I don't mean to be rude here, but this answer does not satisfy the stackoverflow guidelines. This site is supposed to contain answers to questions. It is not LMGTFY. It is very common that I find answers on here that link to pages that no longer exist. What should be done in a stackoverflow answer is to put the relevant part of the linked page into the answer and CITE THE REFERENCE as to not plagiarize. – Bruno Bronosky Apr 17 '14 at 03:33
-
1I concur with Richard - external links might become outdated one day, and this is essentially a way for google. But if we google, then we won't need stackoverflow. – shevy Feb 05 '15 at 10:20
-
As predicted, the IBM Developerwork links is now dead. IBM is kind of bad about this. I'll update the answer with a Wayback archive link from around the time when the question was originally asked. – TheDudeAbides Jul 09 '20 at 19:42
-
1@TheDudeAbides I updated the link with its new URL: https://developer.ibm.com/technologies/linux/tutorials/l-ubuntu-inotify – joschi Jul 11 '20 at 06:45
Below is a snippet of how you can use inotify to watch "aaa". Note that I haven't tested this, I haven't even compiled it! You will need to add error checking to it.
Instead of using a blocking read you can also use poll/select on inotfd.
const char *filename = "aaa";
int inotfd = inotify_init();
int watch_desc = inotify_add_watch(inotfd, filename, IN_MODIFY);
size_t bufsiz = sizeof(struct inotify_event) + PATH_MAX + 1;
struct inotify_event* event = malloc(bufsiz);
/* wait for an event to occur */
read(inotfd, event, bufsiz);
/* process event struct here */
-
1⁺¹ for mention Qt, peoples who looks here from a search engine output could see, that the page have nothing to do with Qt. – Hi-Angel Feb 24 '15 at 11:25
-
The accepted answer doesn't contain `inotify_init()` in the example. This one does. – Deanie Nov 29 '21 at 16:22
-
1
-
If all you need is a commandline application, there is one called inotifywait
that watches files using inotify
from terminal 1
# touch cheese
# while inotifywait -e modify cheese; do
> echo someone touched my cheese
> done
from terminal 2
echo lol >> cheese
here is what is seen on terminal 1
Setting up watches.
Watches established.
cheese MODIFY
someone touched my cheese
Setting up watches.
Watches established.
Update: use with caution and see the comments.

- 10,129
- 5
- 47
- 71
-
5Why use an erroneous and deprecated tool? If you had used it in production, you will feel my pain mate! Any way, look [here](https://stackoverflow.com/questions/47225008/how-to-use-inotifywait-to-watch-files-within-folder-instead-of-folder/49349226#49349226) and [here](https://stackoverflow.com/questions/47673447/inotify-linux-watching-subdirectories/49345762#49345762) for a couple of reasons. – six-k Mar 18 '18 at 16:56
-
"It's a shame that inotifytools is hugely popular and unsuspecting users are unaware of its fallacies." XD nicely worded and cheers to you! – activedecay Mar 18 '18 at 17:09
Since the initial question seems to mention Qt as a tag as noted in several comments here, search engines may have lead you here.
If somebody want to know how to do it with Qt, see http://doc.qt.io/qt-5/qfilesystemwatcher.html for the Qt-version. On Linux it uses a subset of Inotify, if it is available, see explanation on the Qt page for details.
Basically the needed code looks like this:
in mainwindow.h add :
QFileSystemWatcher * watcher;
private slots:
void directoryChanged(const QString & path);
void fileChanged(const QString & path);
and for mainwindow.cpp:
#include <QFileInfo>
#include <QFileSystemWatcher>
watcher = new QFileSystemWatcher(this);
connect(watcher, SIGNAL(fileChanged(const QString &)), this, SLOT(fileChanged(const QString &)));
connect(watcher, SIGNAL(directoryChanged(const QString &)), this, SLOT(directoryChanged(const QString &)));
watcher->addPath("/tmp/"); // watch directory
watcher->addPath("/tmp/a.file"); // watch file
also add the slots in mainwindow.cpp which are called if a file/directory-change is noticed:
void MainWindow::directoryChanged(const QString & path) {
qDebug() << path;
}
void MainWindow::fileChanged(const QString & path) {
qDebug() << path;
}

- 482
- 4
- 21