Home > OS >  How to delete the log messages in a file based on timestamp/ messages older than 7 days should be de
How to delete the log messages in a file based on timestamp/ messages older than 7 days should be de

Time:12-22

How to delete the log messages in a file based on timestamp/ messages older than 7 days should be deleted from file.

Log File Messages Sample-

2022-12-21T09:36:48 00:00 LONSTBYRDEV02 auth_log:

These are lines in the file with time stamp at the start, I just want to delete all the data which are older than 7 days. In this case , considering today's date 22nd December so it should delete all lines before 15th december

Do you know any command or anything which I can include inside the script and remove the lines using loop or something.

Please suggest.

Can anyone help based on above scenario, it should delete all lines older than 15th Dec in this case.

I have written this and further nothing is working.

My COde-

current_DT=$(date  "%m-%d-%y-%T")
echo $current_DT

cutoff=$( date -d "7 days ago"   "%m-%d-%y-%T")
echo $cutoff

while read -r line ; do
  timestamp=$( date -d "$( echo $line)" )
  echo $timestamp
  if [ $timestamp -gt $cutoff ] ; then
    echo "  Timestamp over cutoff old. Deleting lines."
    sed -i "/^$hostname:/d" $hosts
  fi
done

Output I am getting is-

12-22-22-08:17:22
12-15-22-08:17:22

CodePudding user response:

Normally, for logs, you would (as @Gilles Quenot sugested in the comments) use logrotate for your logging. I assume that, for some reason, you need something different.

You can look at it the other way around: not delete everything older, but keep everything 7 days.

#!/bin/bash

logfile="$1"

for i in $(seq 0 6) ; do
    day=$(date  %Y-%m-%d -d -%{i}days)
    grep "^${day}T" "$logfile"
done

CodePudding user response:

Perhaps another approach. Look for newer lines, then stop.

# Set cutoff mark to start of file
mark=0

# Get cutoff time in seconds
cutoff=$( date -d '7 days ago'  %s )

# Loop through file
while read line
do
    # Separate date
    line_date=$( echo $line | cut -d' ' -f1 )

    # Convert to seconds
    line_sec=$( date  %s ${line_date} )

    # If newer, get out of loop
    [ ${line_sec} -gt ${cutoff} ] && break

    # Move mark to next line
    (( mark   ))
done < my.log

# Delete older lines
sed -i "1,${count}d" my.log
  • Related