-1

I m trying to find the unique id of a file which does not change upon modification of file or changing anything which works in multiOS. I cant use the name, path, file content hash as it can be modified.

I tried using inode id, st_ctime_ns but it changes. I need to monitor the file using the id of file system generated.

Changing on file modification:

file_uid = os.stat(file).st_ctime_ns

Changing if reran on another function

file_uid = os.stat(filename).st_ino

Does not work in unix

file_uid = popen(fr"fsutil file queryfileid {file}").read()
  • Can't you use the creation date? – Nineteendo Dec 05 '22 at 08:50
  • https://stackoverflow.com/a/39501288/13454049 – Nineteendo Dec 05 '22 at 08:52
  • 1
    This does not seem to be a programming issue. Questions about OS / file system related things should be asked in one of our sibling sites. – Klaus D. Dec 05 '22 at 08:52
  • not fully sure, but checksum of file might help you – sahasrara62 Dec 05 '22 at 08:53
  • @sahasrara62 "cant use the name, path, file content hash" – Nineteendo Dec 05 '22 at 08:54
  • I think if the answer I linked is correct, it's impossible to get the creation date on Linux. – Nineteendo Dec 05 '22 at 08:55
  • 1
    @Nineteendo creation date is getting changed upon modification of file, also what if the user has created multiple files using a script or something is id going to be unique? – Positive MA Dec 05 '22 at 08:55
  • As I already said, on Linux you don't have access to the creation date, but it "should" be unique for every file created. – Nineteendo Dec 05 '22 at 09:17
  • This doesn't really make sense. If two files have different names, content and creation dates, then *nothing* can tell you that they are the same file, except that *you* consider them to be the same for whatever reason. Anyway, that's not something some independent code might be able to decide. – Thierry Lathuille Dec 09 '22 at 08:48
  • @ThierryLathuille I m not comparing two files, I just needed to figure out how to check if the file has been renamed by comparing the stored name and the new name. I stored the creation date, ino number on the initial and then after renaming rechecked all the ino number/creation dates and compared the file name. I think you misunderstood what I wanted but I figured it out with Nintendo's help. – Positive MA Dec 09 '22 at 09:01
  • That's not what you stated in your question:" I cant use the name, path, file content hash as it can be modified." Your latest comment suggests a much narrower question. – Thierry Lathuille Dec 09 '22 at 09:05
  • @ThierryLathuille "I need to monitor the file using the id of the file system generated." my bad I think I wasn't clear enough. – Positive MA Dec 09 '22 at 09:10

1 Answers1

1

The only real unique identifier would be the creation date, and sadly on some systems it's not available (ctime is the last modified date on Linux).

So this is your best bet: https://stackoverflow.com/a/39501288/13454049 As long as the file is unmodified it will have the same id on Linux.

Code snippet from Mark Amery:

import os
import platform

def creation_date(path_to_file):
    """
    Try to get the date that a file was created, falling back to when it was
    last modified if that isn't possible.
    See http://stackoverflow.com/a/39501288/1709587 for explanation.
    """
    if platform.system() == 'Windows':
        return os.path.getctime(path_to_file)
    else:
        stat = os.stat(path_to_file)
        try:
            return stat.st_birthtime
        except AttributeError:
            # We're probably on Linux. No easy way to get creation dates here,
            # so we'll settle for when its content was last modified.
            return stat.st_mtime
Nineteendo
  • 882
  • 3
  • 18
  • Thank you! I made a little change to this function for Linux, I m returning stat.st_ino instead of modification time. – Positive MA Dec 09 '22 at 09:01