逆引きシェルスクリプト/ファイルの日時を比較する方法
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
* ファイルの日時を比較する方法 [#m95f0f0c]
この記事は、2つファイルを比較し新しいか古いかの日時の比...
使用したシェルはbashになります。
#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)
* ファイルの日時比較のサンプルスクリプト &ref(sample.sh);...
本スクリプトは、1st.txt, 2nd.txtの2つのファイルを作成し...
2nd.txtファイルは1st.txtファイルが作成された後、3秒後に作...
その後、ファイルの日時を比較します。
#!/bin/bash
echo "1st" > 1st.txt
echo "create 1st.txt"
echo "wait 3 sec..."
sleep 3
echo "2nd" > 2nd.txt
echo "create 2nd.txt"
ls --full-time 1st.txt 2nd.txt
if [ 2nd.txt -nt 1st.txt ]; then
echo "2nd.txt is newer than 1st.txt"
fi
if [ 1st.txt -ot 2nd.txt ]; then
echo "1st.txt is older than 2nd.txt"
fi
rm 1st.txt 2nd.txt
* 左辺のファイルが新しい場合 [#q159279b]
左辺のファイルの方が新しく右辺のファイルが古い場合の条件...
(''n''ewer ''t''han とおぼえれば簡単です。)
上記サンプルスクリプトで該当する部分は以下の部分になりま...
if [ 2nd.txt -nt 1st.txt ]; then
echo "2nd.txt is newer than 1st.txt"
fi
* 左辺ファイルが古い場合 [#ee26018b]
左辺のファイルの方が古く右辺のファイルが新しい場合の条件...
(''o''lder ''t''han とおぼえれば簡単です。)
上記サンプルスクリプトで該当する部分は以下の部分になりま...
if [ 1st.txt -ot 2nd.txt ]; then
echo "1st.txt is older than 2nd.txt"
fi
* サンプルスクリプトの実行結果 [#ycbb09c0]
#ref(sample.sh)
以下、この記事のサンプルスクリプトを実行した時の出力です。
$ chmod +x sample.sh
$ ./sample.sh
create 1st.txt
wait 3 sec...
create 2nd.txt
-rw-rw-r-- 1 sakura sakura 4 2012-05-16 11:39:08.0000000...
-rw-rw-r-- 1 sakura sakura 4 2012-05-16 11:39:11.0000000...
2nd.txt is newer than 1st.txt
1st.txt is older than 2nd.txt
* まとめ [#d3cfe694]
|-nt|左辺ファイルが右辺ファイルより新しい場合は真|
|-ot|左辺ファイルが右辺ファイルより古い場合は真|
* 関連資料 [#e3ce7b29]
逆引きシェルスクリプト/if文のサンプルシェルスクリプト・数...
逆引きシェルスクリプト/if文のサンプル・ファイル・ディレク...
-[[ファイルのパーミッションを確認する方法>逆引きシェルス...
-[[シンボリックリンクかどうか判別する方法>逆引きシェルス...
-[[ファイルサイズが0より大きいかを確認する>逆引きシェルス...
#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)
終了行:
#navi(../)
* ファイルの日時を比較する方法 [#m95f0f0c]
この記事は、2つファイルを比較し新しいか古いかの日時の比...
使用したシェルはbashになります。
#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)
* ファイルの日時比較のサンプルスクリプト &ref(sample.sh);...
本スクリプトは、1st.txt, 2nd.txtの2つのファイルを作成し...
2nd.txtファイルは1st.txtファイルが作成された後、3秒後に作...
その後、ファイルの日時を比較します。
#!/bin/bash
echo "1st" > 1st.txt
echo "create 1st.txt"
echo "wait 3 sec..."
sleep 3
echo "2nd" > 2nd.txt
echo "create 2nd.txt"
ls --full-time 1st.txt 2nd.txt
if [ 2nd.txt -nt 1st.txt ]; then
echo "2nd.txt is newer than 1st.txt"
fi
if [ 1st.txt -ot 2nd.txt ]; then
echo "1st.txt is older than 2nd.txt"
fi
rm 1st.txt 2nd.txt
* 左辺のファイルが新しい場合 [#q159279b]
左辺のファイルの方が新しく右辺のファイルが古い場合の条件...
(''n''ewer ''t''han とおぼえれば簡単です。)
上記サンプルスクリプトで該当する部分は以下の部分になりま...
if [ 2nd.txt -nt 1st.txt ]; then
echo "2nd.txt is newer than 1st.txt"
fi
* 左辺ファイルが古い場合 [#ee26018b]
左辺のファイルの方が古く右辺のファイルが新しい場合の条件...
(''o''lder ''t''han とおぼえれば簡単です。)
上記サンプルスクリプトで該当する部分は以下の部分になりま...
if [ 1st.txt -ot 2nd.txt ]; then
echo "1st.txt is older than 2nd.txt"
fi
* サンプルスクリプトの実行結果 [#ycbb09c0]
#ref(sample.sh)
以下、この記事のサンプルスクリプトを実行した時の出力です。
$ chmod +x sample.sh
$ ./sample.sh
create 1st.txt
wait 3 sec...
create 2nd.txt
-rw-rw-r-- 1 sakura sakura 4 2012-05-16 11:39:08.0000000...
-rw-rw-r-- 1 sakura sakura 4 2012-05-16 11:39:11.0000000...
2nd.txt is newer than 1st.txt
1st.txt is older than 2nd.txt
* まとめ [#d3cfe694]
|-nt|左辺ファイルが右辺ファイルより新しい場合は真|
|-ot|左辺ファイルが右辺ファイルより古い場合は真|
* 関連資料 [#e3ce7b29]
逆引きシェルスクリプト/if文のサンプルシェルスクリプト・数...
逆引きシェルスクリプト/if文のサンプル・ファイル・ディレク...
-[[ファイルのパーミッションを確認する方法>逆引きシェルス...
-[[シンボリックリンクかどうか判別する方法>逆引きシェルス...
-[[ファイルサイズが0より大きいかを確認する>逆引きシェルス...
#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)
ページ名: