To find files in current directory bu excluding all sub-directories:
find . -name *.log -maxdepth 1 -print
if -maxdepth is not avaiable
find . \( -name . -o -prune \) -name "*.log" -print
Journals of Oracle Experience
This script is used to archive and compress log files of a specific date. It moves target logs into a sub-directory and compress them. Running multiple instances of the script can archive and compress log files within a date range.
It’s been tested on Aix 5.4 and Linux RHEL 5.5.
#! /usr/bin/ksh #set -x if [ $# -ne 1 ]; then echo Usage: $0 [YYYYMMDD] echo Please give a day on which log files were generated exit 1 fi DAY=$1 #echo DAY=$DAY MAIN_LOG_DIR=/gold/GLP/central/log cd ${MAIN_LOG_DIR} if [ ! -d ${MAIN_LOG_DIR}/LOG_$DAY ]; then mkdir LOG_$DAY if [ $? -ne 0 ]; then echo "Faile to create log file directory '${MAIN_LOG_DIR}/LOG_$DAY'" exit 2 fi fi touch -t "$DAY"0000 /tmp/stdcen$DAY touch -t "$DAY"2359 /tmp/endcen$DAY printf "\nMoving log files to directory LOG_$DAY ...\n" find . \( -name . -o -prune \) -name "*.log" -newer /tmp/stdcen$DAY ! -newer /tmp/endcen$DAY -exec mv {} LOG_$DAY \; printf "\nDone.\n" printf "\nCompressing log files in directory LOG_$DAY ...\n" cd ${MAIN_LOG_DIR}/LOG_$DAY if [ -f LOG_${DAY}.tar.Z ]; then printf "\nCompressed archive file LOG_$DAY.tar.Z already exists. Can not compress.\n" exit 5; fi tar -cvf - *.log |compress > LOG_$DAY.tar.Z res=$? if [ $res -eq 0 ]; then printf "\nCompressed.\n" printf "\nRemoving log files from directory LOG_$DAY ...\n" cd ${MAIN_LOG_DIR}/LOG_$DAY rm -f *.log printf "\nDone.\n\n" else printf "\nError Found when Compressing files in directory LOG_$DAY\n\n" exit 3 fi rm /tmp/stdcen$DAY rm /tmp/endcen$DAY exit 0
Usage:
To archive and comperss log files of Feb 05, 2010
compress.sh 20100205
To archive and comrepress log files from Feb 05 to Feb. 07 of 2010
compress.sh 20100205 &
compress.sh 20100206 &
compress.sh 20100207 &
This script can be launched daily to clean up old log files
1) delete all logs older than 7 days
2) archive and compress logs at age of 3 to 7 days
3) logs of last 2 days remain in original directory
#! /usr/bin/ksh ################################################################################### # # Name: compress_central_logs.sh # # Description: This script is used to clean, archive and compress log files in # the directory "/gold/GLP/central/log". It remove log files older # than 7 days; it archives log files at age between 7 days and 3 days # in a compressed format in a sub-directory LOG_YYYYMMDD and it # leaves log files of last 2 days in current directory. # ################################################################################### MAIN_LOG_DIR=/gold/GLP/central/log SQLPLUS=/gold/GLP/client/oracle/bin/sqlplus ORACLE_HOME=/gold/GLP/client/oracle; export ORACLE_HOME CONN="cenprod/cenprod@glpdb:1521/GOLDPROD" PATH=/gold/GLP/client/oracle/bin:$PATH; export PATH #set -x if [ $# -ne 1 ]; then TODAY=`date +"%Y%m%d"` else TODAY=$1 fi SEVEN_DAYS_AGO=`${SQLPLUS} -S ${CONN} << ENDSTMT1 set echo off pages 0 feedback off alter session set nls_date_format='yyyymmdd'; select to_date('$TODAY', 'yyyymmdd') - 7 from dual; quit; ENDSTMT1` #echo SEVEN_DAYS_AGO=${SEVEN_DAYS_AGO} cd ${MAIN_LOG_DIR} ## Remove log directories older than 7 days for d in `find . -type d -name "LOG_*" -print` do CURR_DAY=`echo $d |cut -d '_' -f 2` if [ $CURR_DAY -lt ${SEVEN_DAYS_AGO} ]; then echo "\nRemoving directory $d ..." rm -Rf $d echo "\nDone." fi done; END_OF_3DAY_AGO=`${SQLPLUS} -S ${CONN} << ENDSTMT2 set echo off pages 0 feedback off alter session set nls_date_format='yyyymmddhh24mi'; select to_date('$TODAY', 'yyyymmdd') - 2 - 1/60/60/24 from dual; quit; ENDSTMT2` #echo END_OF_3DAY_AGO=${END_OF_3DAY_AGO} TWO_DAY_AGO=`${SQLPLUS} -S ${CONN} << ENDSTMT3 set echo off pages 0 feedback off alter session set nls_date_format='yyyymmdd'; select to_date('$TODAY', 'yyyymmdd') - 2 from dual; quit; ENDSTMT3` #echo TWO_DAY_AGO=${TWO_DAY_AGO} cd ${MAIN_LOG_DIR} if [ ! -d ${MAIN_LOG_DIR}/LOG_{$TWO_DAY_AGO} ]; then mkdir LOG_$TWO_DAY_AGO if [ $? -ne 0 ]; then echo "Faile to create log file directory '${MAIN_LOG_DIR}/LOG_${TWO_DAY_AGO}'" exit 2 fi fi touch -t "${END_OF_3DAY_AGO}" /tmp/stdcen${TWO_DAY_AGO} touch -t "${TWO_DAY_AGO}"2359 /tmp/endcen${TWO_DAY_AGO} #ls -l /tmp/stdcen${TWO_DAY_AGO} #ls -l /tmp/endcen${TWO_DAY_AGO} printf "\nMoving log files to directory LOG_${TWO_DAY_AGO} ...\n" find . \( -name . -o -prune \) -name "*.log" -newer /tmp/stdcen${TWO_DAY_AGO} ! -newer /tmp/endcen${TWO_DAY_AGO} -exec mv {} LOG_${TWO_DAY_AGO} \; printf "\nDone.\n" printf "\nCompressing log files in directory LOG_${TWO_DAY_AGO} ...\n" cd ${MAIN_LOG_DIR}/LOG_${TWO_DAY_AGO} if [ -f LOG_${TWO_DAY_AGO}.tar.Z ]; then printf "\nCompressed archive file LOG_${TWO_DAY_AGO}.tar.Z already exists. Can not compress.\n" exit 5; fi tar -cvf - *.log |compress > LOG_${TWO_DAY_AGO}.tar.Z res=$? if [ $res -eq 0 ]; then printf "\nCompressed.\n" printf "\nRemoving log files from directory LOG_${TWO_DAY_AGO} ...\n" cd ${MAIN_LOG_DIR}/LOG_${TWO_DAY_AGO} rm -f *.log printf "\nDone.\n\n" else printf "\nError Found when Compressing files in directory LOG_${TWO_DAY_AGO}\n\n" exit 3 fi rm /tmp/stdcen${TWO_DAY_AGO} rm /tmp/endcen${TWO_DAY_AGO} exit 0