Skip to content

Instantly share code, notes, and snippets.

@ikeisuke
Last active August 19, 2016 05:56
Show Gist options
  • Select an option

  • Save ikeisuke/07c276170b2c6744d096045ab7eba8d1 to your computer and use it in GitHub Desktop.

Select an option

Save ikeisuke/07c276170b2c6744d096045ab7eba8d1 to your computer and use it in GitHub Desktop.
bashのファイルのタイムスタンプ比較処理がよくわからなかったので調べてみた ref: http://qiita.com/ikeisuke/items/2cc48db12baeab932450
[ $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)
#!/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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment