I am creating a generic compilation/transpilation system. One way to know if a file has been compiled/transpiled already would be to compare the source and target file modification dates.
I need to write a bash script that can do that:
source_file=foo;
target_file=bar;
stat_source=$(stat source_file);
stat_target=$(stat target_file);
but how can I extract the dates from the stat output and compare them? Is there a better way than stat
to compare the most recent modification time of a file?
If I call stat on a log file, I get this:
16777220 12391188 -rw-r--r-- 1 alexamil staff 0 321 "Jun 22 17:45:53 2017" "Jun 22 17:20:51 2017" "Jun 22 17:20:51 2017" "Jun 22 15:40:19 2017" 4096 8 0 test.log
AFAICT, the time granularity is no finer than seconds. I need to get something more granular than that if possible.
Best Answer
In testing on this linux system. The usual way to test file times is the shell:
Seems to work with seconds. This, which will touch the files with a time difference less than a second, doesn't detect that difference:
To confirm the issue (time after the dot is nanoseconds):
The
file1
is (a bit) newer thanfile2
.The problem now will be to correctly process the time value.
One solution is to use a formatted output of ls:
Extracting the time to two variables (without the dot):
And compare the times (times with nanoseconds just barely fit in a 64 bit value. If your system does not use 64 bit, this comparison will fail):
That shows that
file1
is newer thanfile2
If
ls
fails to have the format needed, then you may try stat.If the output shows nanoseconds, the we will need date to parse (and format) the time.
The rest is the same, assign the results of file1 and file2 to two variables and numerically compare them.