Does the inode change when renaming or moving a file?
Solution 1
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.
Solution 2
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 ...
Solution 3
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 )
.
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin about 1 year
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?
-
Scott - Слава Україні over 8 yearsChecking 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.