
忘れそうなので、業務で実装したバッチ処理などで利用したshellコマンドのメモ。
随時、追加して行きます。
現在日時からの加減算
# 1時間後 $ echo $(date "+%Y%m%d %H%M%S" -d '1 hour') # 1時間前 $ echo $(date "+%Y%m%d %H%M%S" -d '1 hour ago')
ファイルの拡張子を取得
$ for file in $(find <パス> -type f); do ext=${file##*.}; echo ${ext}; done
最終更新日時 < 30分前のファイルを削除
$ find . -type f -mmin +30 -print0 | xargs -0 --no-run-if-empty rm -f
shellでsendmail
# 関数化 send_mail() { __mail_to=$1 __mail_from=$2 __mail_subject=$3 __mail_message=$4 <send_mail格納先のパス>/sendmail -f "$__mail_from" -t "$__mail_to" << _EOB_ Content-Type: text/plain; charset=euc-jp Content-transfer-encoding: 7bit To: $__mail_to From: $__mail_from Subject: $__mail_subject $__mail_message _EOB_ }
# 送信
$ send_mail ${TO} ${FROM} "<subject>" "<メッセージ>"
# trap
$ trap "send_mail ${TO} ${FROM} \"<subject>\" \"<メッセージ>\"" INT HUP TERM
拡張子を指定したfindの実行結果をtarにアーカイブ
$ find <検索対象パス> ( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" ) -print0 | tar -cvz -T - --null -f <アーカイブファイル名>
特定の拡張子を除外してアーカイブ
$ tar --exclude *.png --exclude *.jpg --exclude *.jpeg -cvf <アーカイブファイル名> <アーカイブ対象パス>
パイプラインで実行した際の終了ステータスを確認したい場合
$ find input/ -type f -print0 | xargs --no-run-if-empty -0 -I {} cp -p {} output/ $ for st in ${PIPESTATUS[@]}; do if [ $st -ne 0 ]; then echo "エラー発生"; fi
ファイルから指定した文字列を削除
$ sed -i -e 's/<削除したい文字列>//g' <対象のファイルパス>
apacheのログ出力をlog rotateに変更
$ sed -i \ -e "s/ErrorLog \"logs\/error_log\"/ErrorLog \"|\/usr\/sbin\/rotatelogs \/var\/log\/httpd\/error\.\%Y\%m\%d\.log 86400 540\"/g" -e "s/CustomLog \"logs\/access_log\" combined /CustomLog \"|\/usr\/sbin\/rotatelogs \/var\/log\/httpd\/access\.\%Y\%m\%d\.log 86400 540\" combined/g" \ /etc/httpd/conf/httpd.conf
ssl証明書内容をopensslで確認
# 証明書ファイルの内容を確認 $ openssl x509 -text -noout -in <サーバ証明書のパス> # 秘密鍵ファイルの内容を確認 $ openssl rsa -text -noout -in <秘密鍵のパス> # CSRファイルの内容を確認 $ openssl req -text -noout -in <CSRファイルのパス>