5

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?

  • 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. – Josef Klimuk May 30 '18 at 9:38
6

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.

  • 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. – Scott Mar 27 '15 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 ...

0

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 ).

-1

An inode used to be the on-disk structure that contained access permissions, ownership, size in bytes, and the disk block numbers of the disk blocks that contained a file's data. So, some metadata, and some data.

The file's name was just an entry in a specially-marked file, called a "directory". The name was associated with the "inode number".

Conceptually, all local filesystems still work this way, although directories are no longer just files with a special mark on them. There's still a separation between an on-disk chunk of metadata (including the data blocks of the file) and a directory entry. Directories for ReiserFS are a B-tree data structure on disk - I'm not sure if there's a B-tree for each directory, or if there's just a database like structure with all directories in it.

I say "local filesystems" because network filesystems like the famous NFS don't have to work that way at all. NetApp appliances can serve the same data with NFS and with CIFS, and the underlying on-disk structure is neither a Unix filesystem, or NTFS or a FAT filesystem.

  • How about a comment about why you downvoted it? Is the above counter-factual, or irrelevant, or are you just a big fan of NTFS and VFAT? – Bruce Ediger Mar 27 '15 at 15:17
  • I assume this has been downvoted because it doesn't seem to actually answer the question "does the inode number change when renaming a file?". – dhag Mar 27 '15 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.