1

I have a file which is written by a Java program periodically.
If it is re-written with exactly the same content, will the file modification time change?
It seems that it doesn't; which surprised me - I wouldn't suppose anything is checking the old content against the new content.

Oops, realised that if the content hasn't changed, the Java program isn't writing the file. My bad. But good to get a definitive answer.

  • Which file system? File systems that do checksumming need to recalculate the checksum anyhow and could recognize if there actually wasn't a change. But this is plain speculation. – Philippos Jul 18 '17 at 10:14
  • File system is xfs – cmgharris Jul 18 '17 at 10:34
  • @Philippos they wouldn’t be POSIX-compliant then. – Stephen Kitt Jul 18 '17 at 11:09
1

Yes, the modification time will change. You can see this yourself:

echo Hello > test
stat test
sleep 2
echo Hello > test
stat test

The second stat will show the same access time as the first (which proves that the file wasn’t deleted and re-created — which of course matches the required behaviour for >), but updated modify and change times.

This is specified by POSIX, e.g. in write():

Upon successful completion, where nbyte is greater than 0, write() shall mark for update the last data modification and last file status change timestamps of the file, and if the file is a regular file, the S_ISUID and S_ISGID bits of the file mode may be cleared.

0

Everytime the file is saved, its modification time changes. If you are saving the file after making changes or just saving the file without making any changes, the modification time changes.

If the time is not changing, most probably the file is not being saved. As you are rewriting the same content again, try checking if the file is getting saved after the content is rewritten.

  • File writing/saving is all being handled by Java library classes, and I don't know what's going on under the hood. I'd be very surprised if it wasn't being saved. – cmgharris Jul 18 '17 at 10:37
0

NOTE: This is not a direct answer to your question, instead it is an answer to the obvious followup question "How do i detect whether a file has actually changed if I can't rely on the timestamp?"

If you ever need to check whether a file's contents (and not just its timestamp) have changed, you have to compare the current contents to previous contents. For example, in sh:

Performing a byte-for-byte comparison with cmp:

if [ -e "$oldcopy" ] && cmp -s "$oldcopy" "$currentcopy" ; then
  # do whatever needs doing if the file hasn't changed
else
  # first do whatever needs doing if the file is new or has changed
  # then make a copy of the current file
  cp -af "$currentcopy" "$oldcopy"
fi

Using checksums aka hashes aka message digests with md5sum:

if [ -e "$filename.md5sum" ] && md5sum --status "$filename.md5sum)" ; then
  # do whatever needs doing if the file hasn't changed
else
  # first do whatever needs doing if the file is new or has changed
  # then save an md5sum of the current file
  md5sum "$filename" > "$filename.md5sum"
fi

md5sum uses more CPU power to generate the message digest for the file. cmp uses more disk space to make a 2nd complete copy of the file.

Any other digest tool (e.g. sha512) can be used instead of md5sum if required...and probably should be if there are any security implications if the files differ.

Java, like many other languages, has libraries to perform message digest calculations.

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.