Linux – Does the inode change when renaming or moving a file

filesfilesystemsinodelinux

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?

Best Answer

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.

Related Question