Score:1

inotify does not track renaming events while watching a single file

in flag

I'm using inotifywait to keep track of events on a single file (instead of a directory), as

$ inotifywait -m -e open -e close -e move myfile.txt

When I open and edit the file, the logs are printed as expected.

Setting up watches.
Watches established.
myfile.txt OPEN 
myfile.txt CLOSE_NOWRITE,CLOSE 

But inotify never emits either MOVED_IN, MOVED_FROM events (not even DELETE_SELF) when I rename the file. On the other hand, if I watch the directory containing myfile.txt, rather than a single file, the events are emitted as usual.

One strange point to me is that, opening and closing the renamed file still log events.

myfile.txt OPEN 
myfile.txt CLOSE_NOWRITE,CLOSE 

At this point, my file is not myfile.txt but has another name. Notably, inotify does not track any event evoked working with a newly created myfile.txt.

My questions are:

  1. How can I correctly track the "renaming" events of a single file with inotifywait, while not watching a full directory?

  2. How can I get the current name of a single file watched? --format "%f" option seems to always print the original file name passed to inotifywait even though the file is renamed, as shown above.

  3. I'm looking for a sort of "rename history logger" that works for a given set of files scattered in the filesystem. Are there any alternatives to inotify, which can be used for this specific purpose?

cn flag
"But inotify never emits either MOVED_IN, MOVED_FROM events (not even DELETE_SELF) when I rename the file." Those seem directory events to me. Moving and deleting ares an alteration of the inode of the file on the directory. It does not alter the file itself. My method would be to watch the directory and filter on filename inside the directory watcher.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.