bashのファイルのタイムスタンプ比較処理がよくわからなかったので調べてみた


bashでファイルのタイムスタンプを比較する -nt, -otに関して動きがよくわからなかったので、調べてみた。

## まとめ
これ使わないほうがいいような気がしてきた

・特殊な場合の動作は以下
ファイルA、Bどちらも存在しない場合：偽
ファイルA、Bどちらかが存在しない場合：新しい方にファイルがあれば真
ファイルA、Bのmtimeが完全に一致する場合：偽
ファイルA、Bのmtimeが秒単位まで一致する場合：偽

## 結果

0が真、1が偽

```
[ $A -nt $B]:
  1: $A and $B do not exist ($A=, $B=)
  0: $A exists, $B does not exist ($A=2016-08-19 14:43:18.214264727 +0900, $B=)
  1: $A dose not exist. $B exists ($A=, $B=2016-08-19 14:43:18.214264727 +0900)
  1: $A is older than $B ($A=2016-08-19 14:43:18.218264799 +0900, $B=2016-08-19 14:43:19.218282884 +0900)
  1: $A is older than $B(0.1sec) ($A=2016-08-19 14:43:19.222282957 +0900, $B=2016-08-19 14:43:19.322284769 +0900)
  0: $A is newer than $B ($A=2016-08-19 14:43:20.326302931 +0900, $B=2016-08-19 14:43:19.326284841 +0900)
  1: $A is newer than $B(0.1sec) ($A=2016-08-19 14:43:20.434304881 +0900, $B=2016-08-19 14:43:20.330303003 +0900)
  1: $A and $B is same timestamp ($A=2016-08-19 14:43:20.434304881 +0900, $B=2016-08-19 14:43:20.434304881 +0900)
[ $A -ot $B]:
  1: $A and $B do not exist ($A=, $B=)
  1: $A exists, $B does not exist ($A=2016-08-19 14:43:20.438304953 +0900, $B=)
  0: $A dose not exist. $B exists ($A=, $B=2016-08-19 14:43:20.442305026 +0900)
  0: $A is older than $B ($A=2016-08-19 14:43:20.442305026 +0900, $B=2016-08-19 14:43:21.446323193 +0900)
  1: $A is older than $B(0.1sec) ($A=2016-08-19 14:43:21.450323265 +0900, $B=2016-08-19 14:43:21.550325069 +0900)
  1: $A is newer than $B ($A=2016-08-19 14:43:22.554343230 +0900, $B=2016-08-19 14:43:21.554325141 +0900)
  1: $A is newer than $B(0.1sec) ($A=2016-08-19 14:43:22.658345114 +0900, $B=2016-08-19 14:43:22.558343302 +0900)
  1: $A and $B is same timestamp ($A=2016-08-19 14:43:22.662345187 +0900, $B=2016-08-19 14:43:22.662345187 +0900)
```

## コード

```bash
#!/bin/bash

A=/tmp/filea
B=/tmp/fileb

function cleanup() {
  [ -f $A ] && rm -f $A
  [ -f $B ] && rm -f $B
}
function show_nt_result() {
  local TIMEA TIMEB
  [ -f $A ] && TIMEA=$(stat $A --format %y)
  [ -f $B ] && TIMEB=$(stat $B --format %y)
  [ $A -nt $B ]
  echo "  $?: $1 (\$A=$TIMEA, \$B=$TIMEB)"
}
function show_ot_result() {
  local TIMEA TIMEB
  [ -f $A ] && TIMEA=$(stat $A --format %y)
  [ -f $B ] && TIMEB=$(stat $B --format %y)
  [ $A -ot $B ]
  echo "  $?: $1 (\$A=$TIMEA, \$B=$TIMEB)"
}

echo '[ $A -nt $B]: '

cleanup
show_nt_result '$A and $B do not exist'

cleanup
touch $A
show_nt_result '$A exists, $B does not exist'

cleanup
touch $B
show_nt_result '$A dose not exist. $B exists'

cleanup
touch $A
sleep 1
touch $B
show_nt_result '$A is older than $B'

cleanup
touch $A
sleep 0.1
touch $B
show_nt_result '$A is older than $B(0.1sec)'

cleanup
touch $B
sleep 1
touch $A
show_nt_result '$A is newer than $B'

cleanup
touch $B
sleep 0.1
touch $A
show_nt_result '$A is newer than $B(0.1sec)'

cleanup
touch $A
touch $B -r $A
show_nt_result '$A and $B is same timestamp'

echo '[ $A -ot $B]: '

cleanup
show_ot_result '$A and $B do not exist'

cleanup
touch $A
show_ot_result '$A exists, $B does not exist'

cleanup
touch $B
show_ot_result '$A dose not exist. $B exists'

cleanup
touch $A
sleep 1
touch $B
show_ot_result '$A is older than $B'

cleanup
touch $A
sleep 0.1
touch $B
show_ot_result '$A is older than $B(0.1sec)'

cleanup
touch $B
sleep 1
touch $A
show_ot_result '$A is newer than $B'

cleanup
touch $B
sleep 0.1
touch $A
show_ot_result '$A is newer than $B(0.1sec)'

cleanup
touch $A
touch $B -r $A
show_ot_result '$A and $B is same timestamp'
```