puk
puk

Reputation: 16782

Watching a file for changes in Linux

Question:

Is there any way to track the progress of a file in Linux (ie. new file/save file)?

Details:

I am aware of inotify which can be used to track entire folders for file creation, deletion, and modification. However, those are very low level. Text editors often when saving a file, write the modified buffer a temporary location, then move it and overwrite the original file. This would be seen by inotify as a CREATE and a MOVE when what I want is a MODIFY.

It seems counter intuitive to me that every time a file is saved it would be interpreted as a new file. Is there any identifying value of a file I can use to distinguish between creating a fresh new file and saving an existing file? Another way to ask this question is: "How do programs such as Beagle, Spotlight, Windows Search, and Google Desktop get around this problem"?

Goal:

Here is a good way to describe what I want: Using Vim, if I open a file and save it (:w), that file will get written to a temporary file and then moved over to the original file, tricking inotify into believing that a completely new file was created and then used to overwrite the original file. However, if add a file to subversion (svn), then open that file with Vim and save it (:w), svn will know that the saved file is actually a modified file and not a new one. How does svn know this?

Upvotes: 3

Views: 1605

Answers (2)

Huygens
Huygens

Reputation: 647

johnshen64 answered you why you don't see it as modified. Regarding SVN (or Git), they would recognize the file as modified because they keep a "key" of already managed files.

So for your database, you would need to do the same. For instance you could use a simple numeric hash from the filename (or the filename itself but string comparison is slow) and do a rapid query to see if he file is already manage before adding it.

Upvotes: 1

johnshen64
johnshen64

Reputation: 3884

I will try to explain while new or a save might look the same in Linux. One big difference in linux from windows is that no file creation time is stored with the inode, only access, modify (file content change), and change (inode change) time is stored. So unless you keep the information elsewhere such as inside the file itself as metadata, you cannot tell if the file was just created or just changed.

Upvotes: 2

Related Questions