Avatar of santoshmotwani
santoshmotwani
Flag for Australia asked on

script to get latest file and email

Hi Experts,

I have a production server ( windows 2008) , there are multiple processes running on that server. If a process fails due to some reason it creates a text file and stores it in a folder called email data.

What i am trying to do here is to write a script that will get the read that latest created txt file from that folder and email it to me.

I have mounted that folder on a ubuntu box.

I have a rough idea on how to get a latest file & read it .


get a latest file - find . -not -type d -printf "%T+ %p\n" | sort -n | tail -1

how to read a file :

FILE="/mnt/emaildata/1.txt"
OUT=$(awk '{ print $2 }' $FILE)
echo "*** error ***"
for ip in $OUT
do
   echo $ip
done


But i am unsure how do i structure the script so that it will only send the latest file created & i want it to run continuously or at least every 30 seconds.

Please help


Thanks
LinuxScripting Languages

Avatar of undefined
Last Comment
farzanj

8/22/2022 - Mon
farzanj

If I am understanding you correctly


You want to read the latest file from a folder

 
while :
do
   folder=/path/to/folder
   file=$(ls -lt $folder | head -1 | awk '{print $1}')

   #email it
   echo "lastest file" | mutt -a $file EMAIL@address
   sleep 30s
done

Open in new window

farzanj

In awk about it would be I think $9.  Try that.  Sorry about that.  If you don't have mutt, you can use mailx or mail, but that would put the text in your body.
Your command would look like
cat $file | mail -s "SUBJECT" email@address
farzanj

To get the latest file I would also try the following command.

files=$(ls -t)
latest=$(${files[0]})
Your help has saved me hundreds of hours of internet surfing.
fblack61
santoshmotwani

ASKER
hi farzani ,


thnx for your help mate,,, but i also want to make sure it is based on creation date, as well as if the file is already sent it should not be sent again..


Hope its clear
farzanj

-t option sorts by modification time and that is probably what you need.  So, if the same file is being written to over and over, you need modification time.  And if a new file is being created, still modification time should work
santoshmotwani

ASKER
lets take out email part.

#!/bin/bash

while :
do
   folder="/mnt/test3/"
   file=$(ls -lt $folder | head -1 | awk '{print $9}')

   #email it
   echo $file
done


this gives me :
galert2.sh: line 5: /tmp/: is a directory
galert2.sh: line 5: /tmp/: is a directory


Please help
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
santoshmotwani

ASKER
oh sorry i changed directory to /tmp instead of /mnt/test3
farzanj

Run the following and send me the output


#!/bin/bash

folder="/mnt/test3/"
file=$(ls -lt $folder)
echo FILES ARE: $file

latest_long=$(ls -l | head -1)
echo LONG IS $latest_long

latest=$(ls -l | awk '{print $9}')
echo $latest
farzanj

The above one is the troubleshooting script to see what is going on
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
santoshmotwani

ASKER
:17 2009-10-12 drwxrwxrwx 2 root staff 0 2010-09-28 17:11 2009-10-09 drwxrwxrwx 2 root staff 0 2010-09-28 17:10 2009-10-08 drwxrwxrwx 2 root staff 0 2010-09-28 17:08 2009-10-07 drwxrwxrwx 2 root staff 0 2010-09-28 17:06 2009-10-06 drwxrwxrwx 2 root staff 0 2010-09-28 17:01 2009-10-05 drwxrwxrwx 2 root staff 0 2010-09-28 17:00 2009-10-02 drwxrwxrwx 2 root staff 0 2010-09-28 16:59 2009-10-01 drwxrwxrwx 2 root staff 0 2010-09-28 16:56 2009-09-30 drwxrwxrwx 2 root staff 0 2010-09-28 16:53 2009-09-29 drwxrwxrwx 2 root staff 0 2010-09-28 16:52 2009-09-28 drwxrwxrwx 2 root staff 0 2010-09-28 16:47 2009-09-25 drwxrwxrwx 2 root staff 0 2010-09-28 16:47 2009-09-26 drwxrwxrwx 2 root staff 0 2010-09-28 16:45 2009-09-24 drwxrwxrwx 2 root staff 0 2010-09-28 16:43 2009-09-23 drwxrwxrwx 2 root staff 0 2010-09-28 16:41 2009-09-22 drwxrwxrwx 2 root staff 0 2010-09-28 16:40 2009-09-21 drwxrwxrwx 2 root staff 0 2010-09-28 16:35 2009-09-18 drwxrwxrwx 2 root staff 0 2010-09-28 16:33 2009-09-17 drwxrwxrwx 2 root staff 0 2010-09-28 16:31 2009-09-16 drwxrwxrwx 2 root staff 0 2010-09-28 16:29 2009-09-15 drwxrwxrwx 2 root staff 0 2010-09-28 16:28 2009-09-14 drwxrwxrwx 2 root staff 0 2010-09-28 16:22 2009-09-11 drwxrwxrwx 3 root staff 0 2010-09-28 16:20 2009-09-10 drwxrwxrwx 2 root staff 0 2010-09-28 16:18 2009-09-09 drwxrwxrwx 2 root staff 0 2010-09-28 16:17 2009-09-08 drwxrwxrwx 2 root staff 0 2010-09-28 16:16 2009-09-07 drwxrwxrwx 2 root staff 0 2010-09-28 16:10 2009-09-04 drwxrwxrwx 2 root staff 0 2010-09-28 16:08 2009-09-03 drwxrwxrwx 2 root staff 0 2010-09-28 16:06 2009-09-02 drwxrwxrwx 2 root staff 0 2010-09-28 16:05 2009-09-01 drwxrwxrwx 2 root staff 0 2010-09-28 16:04 2009-08-31 drwxrwxrwx 2 root staff 0 2010-09-28 15:58 2009-08-28 drwxrwxrwx 2 root staff 0 2010-09-28 15:56 2009-08-27 drwxrwxrwx 2 root staff 0 2010-09-28 15:55 2009-08-26 drwxrwxrwx 2 root staff 0 2010-09-28 15:52 2009-08-25 drwxrwxrwx 2 root staff 0 2010-09-28 15:48 2009-08-24 drwxrwxrwx 2 root staff 0 2010-09-28 15:47 2009-08-21 drwxrwxrwx 2 root staff 0 2010-09-28 15:45 2009-08-20 drwxrwxrwx 2 root staff 0 2010-09-28 15:43 2009-08-19 drwxrwxrwx 2 root staff 0 2010-09-28 15:41 2009-08-18 drwxrwxrwx 2 root staff 0 2010-09-28 15:40 2009-08-17 drwxrwxrwx 2 root staff 0 2010-09-28 15:34 2009-08-14 drwxrwxrwx 2 root staff 0 2010-09-28 15:33 2009-08-13 drwxrwxrwx 2 root staff 0 2010-09-28 15:31 2009-08-12 drwxrwxrwx 2 root staff 0 2010-09-28 15:29 2009-08-11 drwxrwxrwx 3 root staff 0 2010-09-28 15:28 2009-08-10 drwxrwxrwx 2 root staff 0 2010-09-28 15:23 2009-08-07 drwxrwxrwx 2 root staff 0 2010-09-28 15:21 17052010 drwxrwxrwx 2 root staff 0 2010-09-28 15:19 10052010 drwx------ 2 1114 1029 0 2008-08-08 12:48 lost+found
LONG IS total 6276
farzanj

Ok, run this please:

#!/bin/bash

folder="/mnt/test3/"
latest_long=$(ls -lt $folder | find -type f | head -1)

echo LATEST LONG: $latest_long
echo

latest=$(ls -l | awk '{print $9}')
echo LATEST:  $latest
santoshmotwani

ASKER
LATEST LONG: ./var/lock/evms-engine

LATEST: -> -> -> -> ->
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
farzanj

Sorry replace the last two lines

latest=$(echo $latest_long | awk '{print $9}')
echo "LATEST : $latest"
santoshmotwani

ASKER
LATEST LONG: ./var/lock/evms-engine
LATEST :
farzanj

Sorry one more, unfortunately, I don't have a terminal myself


#!/bin/bash

folder="/mnt/test3/"
latest_long=$(ls -lt $folder | grep -v "^d" | head -1)

echo LATEST LONG: $latest_long
echo

latest=$(echo $latest_long | awk '{print $8}')
echo "LATEST : $latest"
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
santoshmotwani

ASKER
LATEST LONG: total 928

LATEST :
farzanj

One last

#!/bin/bash

folder="/mnt/test3/"
latest_long=$(ls -lrt $folder | grep -v "^d" | tail -1)

echo LATEST LONG: $latest_long
echo

latest=$(echo $latest_long | awk '{print $8}')
echo "LATEST : $latest"
santoshmotwani

ASKER
LATEST LONG: -rwxrw-rw- 1 1070 root 0 2011-03-10 13:35 New Text Document.txt

LATEST : New


some success now all we want to see whts in tht file .... appreciate your help
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
farzanj

You know what, just do awk '{print $NF}'

So change the second last line to

latest=$(echo $latest_long | awk '{print $NF}')


Sorry, was too tired and don't even have a terminal in front of me
santoshmotwani

ASKER
still unable to display file contents
farzanj

Hi, it is morning and I can think now.
I thought you would assemble it now.  But it is alright.  Here it goes.

If you want to display the contents of the file once, here is the code.
 
cat $(ls -Ft | grep -v '/' | head -1)

Open in new window


If however you want to send it via email, in the body and only once, then:
 
cat $(ls -Ft | grep -v '/' | head -1) | mail -s "Latest file" email_address@domain.com

Open in new window


If however, you want to repeat this process every 30 seconds, this is what you would write
while :
do
   cat $(ls -Ft | grep -v '/' | head -1) | mail -s "Latest file" Email_Address@domain.com
   #wait for 30 seconds
   sleep 30s
done

Open in new window


I hope this is what you wanted.
Cheers
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
santoshmotwani

ASKER
Hi,

good stuff only one issue.... there are all.txt files in there.... and i get this error when i run this script.

cat: 5660.TXT*: No such file or directory

Any input?
ASKER CERTIFIED SOLUTION
farzanj

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
santoshmotwani

ASKER
Sorry mate for being a such a pain... but u did a gr8 job ......
farzanj

Glad to help
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.