inotify_add_watch(2)
π inotify_add_watch β bestie's guide to the inotify_add_watch(2) system call π
okay so picture this: it's a System Calls Manual page, and today's main character is inotify_add_watch. she's the one who actually puts a watch on stuff for you. let's get into it π
β¨ LIBRARY β¨
standard C library β that's libc, and yeah, you link it with -lc. iconic, as always π
πΈ SYNOPSIS πΈ
#include <sys/inotify.h>
int inotify_add_watch(int fd, const char *path, uint32_t mask);
that's it. three little args. so much power π¦
π DESCRIPTION π
so inotify_add_watch() does one of two things, depending on the vibe:
- adds a new watch, or
- modifies an existing watch
β¦for the file whose location lives in path. and heads up bestie β the caller must have read permission for that file. no read permission, no watch. periodt π₯
the fd argument is a file descriptor that points at the inotify instance whose watch list you're about to mess with. that's the instance getting modified.
the events you wanna monitor for path? those go in the mask bit-mask argument. for a full rundown of which bits can be set in mask, go peek at inotify(7) β she has the lore π
now for the good part β¨ A successful call to inotify_add_watch() returns a unique watch descriptor for this inotify instance β specifically for the filesystem object (the inode) that corresponds to path. here's the tea:
- if that filesystem object wasn't already being watched by this inotify instance, the watch descriptor is newly allocated β fresh out the box π
- if that filesystem object was already being watched (maybe via a different link to the same object, iykyk), then you get the descriptor for the existing watch back. no duplicates, she's efficient π
and where does that watch descriptor show up later? it gets returned by later read(2)s from the inotify file descriptor π those reads fetch inotify_event structures (again, see inotify(7) for the details) that indicate filesystem events β and the watch descriptor inside that structure is what tells you which object the event happened for. it's giving detective work π
π RETURN VALUE π
- on success,
inotify_add_watch()returns a watch descriptor β a nonnegative integer π - on error,
-1comes back anderrnois set to tell you exactly what went wrong π
π ERRORS π
EACCESβ read access to the given file is not permitted. bestie you cannot read it, she said no πEBADFβ the given file descriptor is not valid.EEXISTβmaskcontainsIN_MASK_CREATEandpathrefers to a file already being watched by the samefd. she's already on the list, girl πEFAULTβpathpoints outside of the process's accessible address space.EINVALβ one of these three situations: the given event mask contains no valid events; ormaskcontains bothIN_MASK_ADDandIN_MASK_CREATE; orfdis not an inotify file descriptor. pick your poison π§ENAMETOOLONGβpathis too long. she's rambling πENOENTβ a directory component inpathdoes not exist, or it's a dangling symbolic link.ENOMEMβ insufficient kernel memory was available.ENOSPCβ the user limit on the total number of inotify watches was reached, or the kernel failed to allocate a needed resource. you maxed out, no cap π«ENOTDIRβmaskcontainsIN_ONLYDIRandpathis not a directory. read the room π
π STANDARDS π
Linux. that's the whole standards section. short, sweet, unbothered π
β HISTORY β
Linux 2.6.13. vintage but thriving, a true original π
π«Ά EXAMPLES π«Ά
See inotify(7). she's got the examples, go say hi π
πΈ SEE ALSO πΈ
and that's the whole page, bestie π this was Linux man-pages version 6.20, dated 2026-02-08, and it's officially a wrap on inotify_add_watch(2) π slay, go watch some files β¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: GPL-2.0-or-later
- Copyright 2005, Robert Love <rlove@rlove.org>
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms