简体   繁体   中英

How to Regex in a script to gzip log files

i would like to gzip log files but i cannot work out how to run a regex expression in my command.

My Log file look like this, they roll every hour.

-rw-r--r-- 1 aus nds    191353 Sep 28 01:59 fubar.log.20150928-01
-rw-r--r-- 1 aus nds    191058 Sep 28 02:59 fubar.log.20150928-02
-rw-r--r-- 1 aus nds    190991 Sep 28 03:59 fubar.log.20150928-03
-rw-r--r-- 1 aus nds    191388 Sep 28 04:59 fubar.log.20150928-04

script.

FUBAR_DATE=$(date -d "days ago"  +"%Y%m%d ")
fubar_file="/apps/fubar/logs/fubar.log."$AUS_DATE"-^[0-9]"
/bin/gzip $fubar_file

i have tried a few varients on using the regex but without success, can you see the simple error in my code. Thanks in advace

Why not make fubar_file an array to hold the matching log file names, and then use a loop to gzip them individually. Then presuming AUS_DATE contains 20150928 :

# FUBAR_DATE=$(date -d "days ago"  +"%Y%m%d ") # not needed for gzip
fubar_file=( /apps/fubar/logs/fubar.log.$AUS_DATE-[0-9][0-9] )
for i in ${fubar_file[@]}; do
    gzip "$i"
done

or if you do not need to preserve the filenames in the array for later use, just gzip the files with a for loop:

for i in /apps/fubar/logs/fubar.log.$AUS_DATE-[0-9][0-9]; do
    gzip "$i"
done

or, simply use find to match the files and gzip them:

find /apps/fubar/logs -type f -name "fubar.log.$AUS_DATE-[0-9][0-9]" -execdir gzip '{}' +

Note: all answers presume AUS_DATE contains 20150928 .

I did:

$ fubar_file="./fubar.log."${FUBAR_DATE%% }"-[0-9][0-9]"

and it worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM