How to verify that file2 is newer than file1 in bash?

Found it here

for f in /abcd/xyz* do
   [ "$f" -nt /abcd/test.txt ] && echo "file f$ found" done

if [[ FILE1 -nt FILE2 ]]; then
  echo FILE1 is newer than FILE2
fi

Taken from 'man test'. Excerpt:

FILE1 -nt FILE2
  FILE1 is newer (modification date) than FILE2

Another way to do this:

find -name file2 -newer file1

will return null if file2 is older or the same age as file1. It will return the name (and directory) of file2 if it's newer.

Be aware that Linux doesn't keep track of when files were created. These tests will be for the most recent modification date and time.

Tags:

Bash