2012-02-13 34 views
0

我試圖做這樣的事情:如何根據文件日期的比較條件執行bash代碼?

for f in $SOMEDIR 
    do 
     if "$f is newer than some other file" 
     then 
      "do this" 
     else 
      "do this" 
     fi 
    done 
fi 

我知道我已經看到了一個很好的和簡單的方法來快速比較文件日期和執行,如果第一個文件的日期恰好是比第二,但較新我不知道在哪裏尋找再找到。

任何幫助將是偉大的!

回答

2

使用文件測試運算符。

來自http://tldp.org/LDP/abs/html/fto.html

F1 F2 -nt
文件F1比F2新

F1 F2 -ot
文件F1比F2

假設測試老年人和test2的是文件。

if [[ "test" -nt "test2" ]]; then 
    echo "yes" 
else 
    echo "no" 
fi 

if [[ "test" -ot "test2" ]]; then 
    echo "yes" 
else 
    echo "no" 
fi 

伏法:

$ ./test 
yes 
no 
+0

這個美麗的工作!謝謝! – djdrzzy 2012-02-13 22:55:06