[LINUX] Batch thought by beginners

A log file named yyyymmdd. *. Log has been accumulated, so the logs with the same date in the directory are consolidated.

!/bin/bash

CMDNAME=`basename $0`

#Error if no argument is confirmed
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

#Take the working directory from the argument
d="$1"

#The entire working directory
FILES="$1"*

echo "work on directory $d"

#loop
for f in $FILES
do
  echo "Processing $f"

  #Extract date only
  #$f(/*/*/*/*/yyyymmdd.*.log)First of all
  #In front and back match/Take the rest yyyymmdd.*.from log
  #In front of the front match., Only yyyymmdd, and assign to file
  file=$(echo "${f##*/}" | cut -f 1 -d '.')

#Check the existence of the file just in case
#If there is, group by date.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

Recommended Posts

Batch thought by beginners
Typing automation notes by Python beginners
Answer to AtCoder Beginners Selection by Python3
Django tutorial summary for beginners by beginners ③ (View)
Django tutorial summary for beginners by beginners ⑤ (test)
A textbook for beginners made by Python beginners