10

in PHP the fileinode() functions returns the inode of a file. I was wondering if I can use it to determine if a file was renamed, moved or modified.

I did some tests and it seems the inode stays the same after rename. Is this behavior consistent? Does it work for any type of file, on any linux distribution?

1
  • Moving is actually creating a copy of a file on new location, then deleting the file in the previous location. So, the question is: does copying of a file effect it's inode / inode number. Since it is impossible to have two identical inode numbers at the same time, so there should be a change of inode number after copying and before deleting the old file (short time). I discovered that the inode number does not change after mv command, so, probably the inode number of the deleted file was assigned to the copyied file after the deletion. May 30, 2018 at 9:38

3 Answers 3

10

A file rename that doesn't cross file system boundaries is just a metadata change, so it should preserve the inode number. Generally speaking, opening a file and modifying its contents should not change its inode number, which only makes sense within a single file system anyway (but it will change the access times, for example). Note that some tools such as text editors will tend to create a brand new file rather than write in place, and that would cause a new inode to be used.

If your goal is to check files for changes, checking the access times and size could be more reliable.

1
  • 2
    Checking access time will lead to false positives; checking size will lead to false negatives. To check whether a file has been modified (its contents have been altered), look at mtime. To see whether any aspect of a file (contents or attributes) have changed, use ctime. Mar 27, 2015 at 14:25
3

You can't use inode to check if a file has been changed.

It may or may not change when a file is renamed, or moved. It will typically stay the same unless moved onto another disk ...

1

The PHP documentation sucks is very bad: vague, ambiguous, and misleading.  fileinode() is tersely defined as “gets file inode” or “returns the inode”.  But if you dig a little deeper, the documentation seems to start saying that this function returns the inode number.  An inode is more than an inode number.  The difference between “returning the inode of a file” and “getting the inode number of a file” is comparable to the difference between my signing the deed of my house over to you versus my telling you my address.

dhag’s and robert’s answers are very good, so I won’t repeat what they said or try to improve on them.  Except to add: the best way to detect whether a file has been renamed or moved within a filesystem is to look at the st_ctime member of the inode structure.  This is the time (Unix timestamp: date & time) of the last change to the file, including its attributes.  According to the documentation, you can get this in PHP with int filectime ( string $filename ).

You must log in to answer this question.

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