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.logAFAICT, the time granularity is no finer than seconds. I need to get something more granular than that if possible.
Compare two file modification dates
files;timestamps;stat
Given that you're using the stat (similar functionality, but different output format on BSDs and GNU), you could also use the test utility, which does this comparison directly:   FILE1 -nt FILE2          FILE1 is newer (modification date) than FILE2   FILE1 -ot FILE2          FILE1 is older than FILE2In your example,if [ $source_file -nt $target_file ]then    printf '%s\n' $source_file is newer than $target_filefiThe feature is not available in POSIX (see its documentation for test), which provides as a rationale:Some additional primaries newly invented or from the KornShell appeared in an early proposal as part of the conditional command ([[]]): s1 > s2, s1 < s2, str = pattern, str != pattern, f1 -nt f2, f1 -ot f2, and f1 -ef f2. They were not carried forward into the test utility when the conditional command was removed from the shell because they have not been included in the test utility built into historical implementations of the sh utility.That might change in the future though as the feature is widely  supported.Note that when operands are symlinks, it's the modification time of the target of the symlink that is considered (which is generally what you want, use find -newer instead if not). When symlinks cannot be resolved, the behaviour between implementations (some consider an existing file as always newer than one that can't resolve, some will always report false if any of the operands can't be resolved).Also note that not all implementations support sub-second granularity (bash's test/[ builtin as of version 4.4 still doesn't for instance, GNU test and the test builtin of zsh or ksh93 do, at least on GNU/Linux).For reference:For the GNU test utility implementation (though note that your shell, if fish or Bourne-like, will also have a test/[ builtin that typically shadows it, use env test instead of test to bypass it), get_mtime in test.c reads struct timespec, andoption -nt uses that data