逆引きシェルスクリプト/ファイルの日時を比較する方法
の編集
Top
/
逆引きシェルスクリプト
/
ファイルの日時を比較する方法
[
トップ
] [
編集
|
差分
|
バックアップ
|
添付
|
リロード
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
-- 雛形とするページ --
(no template pages)
#navi(../) * ファイルの日時を比較する方法 [#m95f0f0c] この記事は、2つファイルを比較し新しいか古いかの日時の比較を行うサンプルスクリプトになります。~ 使用したシェルはbashになります。 #contents #htmlinsertpcsp(linux_ads_top.html,linux-sp.html) * ファイルの日時比較のサンプルスクリプト &ref(sample.sh); [#ed7a48ae] 本スクリプトは、1st.txt, 2nd.txtの2つのファイルを作成します。~ 2nd.txtファイルは1st.txtファイルが作成された後、3秒後に作成されます。(sleepコマンドを利用)~ その後、ファイルの日時を比較します。 #!/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] 左辺のファイルの方が新しく右辺のファイルが古い場合の条件式は''-nt''になります。~ (''n''ewer ''t''han とおぼえれば簡単です。) 上記サンプルスクリプトで該当する部分は以下の部分になります。 if [ 2nd.txt -nt 1st.txt ]; then echo "2nd.txt is newer than 1st.txt" fi * 左辺ファイルが古い場合 [#ee26018b] 左辺のファイルの方が古く右辺のファイルが新しい場合の条件式は''-ot''になります。 (''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.000000000 +0900 1st.txt -rw-rw-r-- 1 sakura sakura 4 2012-05-16 11:39:11.000000000 +0900 2nd.txt 2nd.txt is newer than 1st.txt 1st.txt is older than 2nd.txt * まとめ [#d3cfe694] |-nt|左辺ファイルが右辺ファイルより新しい場合は真| |-ot|左辺ファイルが右辺ファイルより古い場合は真| * 関連資料 [#e3ce7b29] 逆引きシェルスクリプト/if文のサンプルシェルスクリプト・数値比較編 逆引きシェルスクリプト/if文のサンプル・ファイル・ディレクトリ編 -[[ファイルのパーミッションを確認する方法>逆引きシェルスクリプト/ファイルのパーミッションを確認する方法]] -[[シンボリックリンクかどうか判別する方法>逆引きシェルスクリプト/シンボリックリンクかどうか判別する方法]] -[[ファイルサイズが0より大きいかを確認する>逆引きシェルスクリプト/ファイルサイズが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); [#ed7a48ae] 本スクリプトは、1st.txt, 2nd.txtの2つのファイルを作成します。~ 2nd.txtファイルは1st.txtファイルが作成された後、3秒後に作成されます。(sleepコマンドを利用)~ その後、ファイルの日時を比較します。 #!/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] 左辺のファイルの方が新しく右辺のファイルが古い場合の条件式は''-nt''になります。~ (''n''ewer ''t''han とおぼえれば簡単です。) 上記サンプルスクリプトで該当する部分は以下の部分になります。 if [ 2nd.txt -nt 1st.txt ]; then echo "2nd.txt is newer than 1st.txt" fi * 左辺ファイルが古い場合 [#ee26018b] 左辺のファイルの方が古く右辺のファイルが新しい場合の条件式は''-ot''になります。 (''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.000000000 +0900 1st.txt -rw-rw-r-- 1 sakura sakura 4 2012-05-16 11:39:11.000000000 +0900 2nd.txt 2nd.txt is newer than 1st.txt 1st.txt is older than 2nd.txt * まとめ [#d3cfe694] |-nt|左辺ファイルが右辺ファイルより新しい場合は真| |-ot|左辺ファイルが右辺ファイルより古い場合は真| * 関連資料 [#e3ce7b29] 逆引きシェルスクリプト/if文のサンプルシェルスクリプト・数値比較編 逆引きシェルスクリプト/if文のサンプル・ファイル・ディレクトリ編 -[[ファイルのパーミッションを確認する方法>逆引きシェルスクリプト/ファイルのパーミッションを確認する方法]] -[[シンボリックリンクかどうか判別する方法>逆引きシェルスクリプト/シンボリックリンクかどうか判別する方法]] -[[ファイルサイズが0より大きいかを確認する>逆引きシェルスクリプト/ファイルサイズが0より大きいかを確認する]] #htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)
テキスト整形のルールを表示する
添付ファイル:
sample.sh
698件
[
詳細
]