0

I am try to compare the two time file based on their modify/lastchange time stamp & then copy the file to some folder code below

#!/bin/sh
cd /sapmnt/audit/
echo $PWD
t1= stat -c %z audit.log20151019115132
cd /var/log/audit/
echo $PWD
t2= stat -c %z audit.log.3
echo $t1
echo $t2
if [ $t2 -nt $t1 ];
then
echo "PASS"
else
   echo "Fail"
fi

output:

/sapmnt/audit
2015-10-19 11:51:32.189657200 +0000
/var/log/audit
2015-10-20 17:25:01.000000000 +0000


PASS

The problem is that it always output Pass either if i exchange the condition to the [ $t1 -nt $t2 ].

Sven
  • 97,248
  • 13
  • 177
  • 225

2 Answers2

1

-nt and -ot work directly on files, not on values.

#!/bin/sh

t1='/sapmnt/audit//audit.log20151019115132'
t2='/var/log/audit/audit.log.3'
echo $t1
echo $t2
if [ $t2 -nt $t1 ];
then
echo "PASS"
else
   echo "Fail"
fi
Sven
  • 97,248
  • 13
  • 177
  • 225
1

according to man test

FILE1 -nt FILE2

FILE1 is newer (modification date) than FILE2

-nt operator will compare file, not timestamp.

Archemar
  • 1,341
  • 11
  • 19