Compare log files and get latest

I have a directory location where in some logs gets generated whenever some application build is triggered.
I need to send the generated log as an email to end user.
I will get files like abcyyyy_mm_dd_time.log and next file will have different time for same day.
Need to run a cron and take the latest file and email.
Need help how will I compare and get the latest file each time and send it across.
Any help asap will be highly appreciated ..

Below will always give you the latest

ls -1t abc*.log | head -1

Rest is just little coding.

Hi,

Below command can be used to take 5 recently modified or created files

  ls -1t dir/*|head -5

Thanks for the Info but ls -lt | abc.log | head -1 will give the latest .
But say in last 10 minutes 3 logs got generated then I need to get those 3 and send too.
In a nutshell whatever gets created after I last sent the log file needs to be captured and sent...:slight_smile:

In such a case if abc.123.log is the last file I have sent
I will list down all the files with ls -lt | abc.*.log
and find the position of abc.123.log in the listing
and now head command with little alteration in -count can give me names of all the files.
Try it out... :slight_smile:

btw it was 1(one) and not l(ell)

Make file prevstamp = when you have done mail process. Find those files which are newer.

#!/bin/ksh
smail()
{
fn="$1"
/usr/lib/sendmail -t -i <<EOF
From: myemail
To: youremal
Subject: $fn

Here is file $fn
$(cat $fn)
EOF
}

##main##
[ ! -f prevstamp ] && touch -m 20010101120000 prevstamp
touch newstamp
find . -maxdepth 1  -name "*.log" -newer prevstamp | while read fname
do
        smail "$fname"
done
cp -fp newstamp prevstamp 2>/dev/null
rm -f newstamp 2>/dev/null

Thanks for the script but why are you doing this here

20010101120000 prevstamp

Please explain...

find use prevstamp, if you not have it, you must create the first. You can set timestamp as you like, 200101... is only example. Example you don't like to look old logs, starting from just now:
touch prevstamp
is good solution.

Try this:

ls -1tr | sed -n '1,/^lastlog.txt$/! p' > newlog.txt
mv newlog.txt lastlog.txt

You have the list of latest files lastlog.txt
Do your usual email procedure.

This makes sense edidataguy but if the log gets created then we can do ls -ltr /abc/def/abcd_yymmdd.log .
Compare and move it to new log and then move old lo to new log .
Incase the log does not gets created then the script which will be running through cron each 10 minutes will give an error each time it does not find a log .
Scenario is the log files will be created randomly 3-4 at a time when application triggers and it may not get generated at all for 2-3 days .

@yogi90 : you didn't provide the time format.

I am assuming it is HH_MM

$ls -1
abc2008_07_22_13_25.log
abc2009_07_22_12_23.log
abc2009_07_22_13_25.log
abc2009_07_21_11_25.log

$sort -t'_' -k1.4,1.7 -k2.1,2.2 -k3.1,3.2 -k4.1,4.2 -k5.1,5.2 | tail -1
$abc2008_07_22_13_25.log

I wish I understood the requirement correctly.

The log file will always be created, provided the log file already exists.
If you list and no files exist, then a 0 byte file will be created.
Try this:

echo "bla bla bla" > oldlog.log
ls -l oldlog.log

output

-rw-r--r--  1 admin None        12 Jul 23 13:25 oldlog.log

Now try this:

 ls -1 idontexist* > oldlog.log
ls -l oldlog.log

output

-rw-r--r--  1 admin None        0 Jul 23 13:25    oldlog.log

If you want in your code check for [[ -s oldlog.log ]]

#!/usr/bin/ksh

###############################################################################
# #
#-----------------------------------------------------------------------------#
# Shell Script Name : getlogs #
# #
# Location (directory) : /usr/local/bin #
#-----------------------------------------------------------------------------#
# #
# #
###############################################################################

#typeset -u export SID=$1
#typeset -l export sid=$1

Hostname=$(hostname)

usermail="abcd @pqrst.com"

ls -1tr /dir1/dir2/abcd*.log | sed -n '1,/^lastlog.txt$/! p' > newlog.txt

mv newlog.txt lastlog.txt

sleep 30

/usr/bin/mailx -s "${HOSTNAME}: Subject" $usermail < $/dir1/dir2/newlog.txt

edidataguy if I use this it will always send a zero byte file to user which I do not want , I need to send the log file only when it gets created sometimes otherwise do not send anything

find logs modified ten minutes ago. just modify your script accordingly. if there is nothing found then there are no log files generated within 10 minutes timeframe then don't send email.

find . -name "*.log" -mmin +10

#!/usr/bin/ksh

###############################################################################
# #
#-----------------------------------------------------------------------------#
# Shell Script Name : getlogs #
#
# Location (directory) : /usr/local/bin #
#-----------------------------------------------------------------------------#
# #
###############################################################################

#typeset -u export SID=$1
#typeset -l export sid=$1

Hostname=$(hostname)

usermail="abcd@pqrst.com"

if [ "`find . -name "/dir1/dir2/abcd*.log" -mmin 10 | wc -l`" -gt "0" ]
then
ls -1tr /dir1/dir2/abcd*.log | sed -n '1,/^lastlog.txt$/! p' > newlog.txt

mv newlog.txt lastlog.txt

sleep 30

/usr/bin/mailx -s "${HOSTNAME}: subject" $usermail < $/dir1/dir2/newlog.txt
fi

When I run this script it gives an error ./getISTlogsnew.ksh
find: bad option -mmin

Please advise ..

Hi Yogi,

you can get latest file as

ls -1tr abc* | tail -1

scripter

Please use code tags.

your find doesn't support mmin option.
you can make a ref timestamp with touch.

with the reference timestamp file, you can use test FILE1 -ot FILE2 (is older than) to make the determination.

Please read my message fully.
I had said at the bottom:

If you want in your code check for [[ -s oldlog.log ]]

Use an "if" command and check if the file size is not 0.