0
1

More than 3 years have passed since last update.

バッチ初心者が考えたバッチ

Posted at

yyyymmdd.*.logと命名されたログファイルが溜まったので、ディレクトリ内にある同日付の付いたログを固める

!/bin/bash

CMDNAME=`basename $0`

#引数の確認 なければエラー
if [ $# -ne 1 ]; then
  echo "need a directory after the command" 1>&2
  echo "Ex. /var/log/10.0.0.254/" 1>&2
  exit 1
fi

#作業ディレクトリを引数から取る
d="$1"

#作業ディレクトリ内全体
FILES="$1"*

echo "work on directory $d"

#ループ
for f in $FILES
do
  echo "Processing $f"

  #日付のみ抽出
  #$f(/*/*/*/*/yyyymmdd.*.log)からまず
  #前方最後方一致で/を取り、残りのyyyymmdd.*.logから
  #前方最前方一致で.を取り、yyyymmddのみにし、fileへ代入
  file=$(echo "${f##*/}" | cut -f 1 -d '.')

#念のためファイルの存在確認
#あれば日付でまとめて.tar.gz化
if [ -f "$f" ]; then
  tar czf $d$file.log.tar.gz  $d$file.*
#  echo "$d$file.log.tar.gz  $d$file.*"
fi

done

exit 0
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1