newer-file.sh 425 Bytes
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/env bash

# Returns 0 if the first file is newer than the second file
# Works on files or directories

if [[ ! -e "$1" ]]; then exit 1; fi
if [[ ! -e "$2" ]]; then exit 1; fi

9 10 11 12 13 14
if uname | grep -q "Darwin"; then
    MOD_TIME_FMT="-f %m"
else
    MOD_TIME_FMT="-c %Y"
fi

15 16
FILE_1_AGE=$(stat "$MOD_TIME_FMT" "$1")
FILE_2_AGE=$(stat "$MOD_TIME_FMT" "$2")
17 18

if [ "$FILE_1_AGE" -gt "$FILE_2_AGE" ]; then
19 20
  exit 0
fi
21 22

exit 1