0

I'd like to be able to detect when a file is considered different if the data are still the same but the ownership has changed (or the permission, access/creation time, etc...).

Is there a such tool ?

Z0OM
  • 3,149
A.G.
  • 113

2 Answers2

3

As stated in the answer provided by Detect changes in permissions, you can determine the time of permission changes and the actual permissions by using the stat command.

So something like that should work:

stat -c "%a %Z" file | cat - file | sha1sum
0
tar -cf - --no-recursion the_file | sha256sum

Notes:

  • --no-recursion in case the_file is of the type directory. I assumed you want to detect changes to the file itself, not to its subdirectories and such.
  • tar does not store atime. This means the method cannot detect any change in atime of the_file. On the other hand the sole act of reading the_file by tar may update its atime (depending on mount options), so ignoring atime is most likely a good thing.
  • If there's a problem with tar, you will still get some digest (the digest of empty input) and exit status 0 from sha256sum. See Get exit status of process that's piped to another.
  • tar stores the path. The method is sensitive not only to the actual name of the file, but primarily to the pathname you provide. E.g. specifying ./the_file will give you a different digest than the_file. To reliably detect changes, pick a pathname and stick to it.
  • You can use another *sum tool (e.g. md5sum). Pick one and stick to it.
  • thanks but changing the filename can't be used to compute a digest. – A.G. Jun 21 '23 at 12:53
  • @A.G. Well, I had to guess if "others" in the title and "etc." in the body should include the filename. I guessed wrong. – Kamil Maciorowski Jun 21 '23 at 12:56
  • Sorry for not precising but I was thinking it was obvious that the filename is never included in a digest since it refers to a different file. – A.G. Jul 18 '23 at 14:50