Determine more recent of two files in bash

Alec Jacobson

February 18, 2011

weblog/

Here's a simple bash script that determines the more recent of two files":
#!/bin/bash
# Usage:
#    morerecent file1 file2
if [[ `stat -f %c "$1"` > `stat -f %c "$2"` ]];
then
  echo "$1"
else
  echo "$2"
fi
Note: It seems stat is infamously implementation dependent so the format/parameters may be different for your machine.