Operating System - HP-UX
1838469 Members
2844 Online
110126 Solutions
New Discussion

conditional mail on find?

 
SOLVED
Go to solution
Anna Fong
Advisor

conditional mail on find?

I want to send myself an email only if find cmd locates a file over a certain size.

I tried this but it always sends a message, which isn't what I want.

find / \( -name myLog.out' \) -size +1c -exec cat {} \; | mail user@domain

Is what I'm attempting even possible without having to create a script?
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor
Solution

Re: conditional mail on find?

Hi Anna:

Well, I suppose anything that fits on a command line isn't a script (or is it?) :-))

Try this:

# find /tmp \( -name 'myLog.out' -a -size +0c \) |[ `wc -c` != 0 ] && mailx -s "BigFile" root < /dev/null

Regards!

...JRF...
Wilfred Chau_1
Respected Contributor

Re: conditional mail on find?

find / -name myLog.log -size +c -exec cat {} \; |mailx -s "File is too big" user@domain
James R. Ferguson
Acclaimed Contributor

Re: conditional mail on find?

Hi (again) Anna:

You will need to change my example to fit your needs (of course). That is, I offered a working "script" that probes the /tmp directory for a file named "myLog.out" with a size of zero bytes -- quite convenient for testing the logic...

Regards!

...JRF...
Anna Fong
Advisor

Re: conditional mail on find?

Wilfred,

Your example does the same as mine -- always sending a file. I want the mail to be conditional. Nice try.
Anna Fong
Advisor

Re: conditional mail on find?

James,

Yes, thanks. Did modify your example for my needs. In fact, wanted the contents of the log sent to me, so here is what I did --

find / \( -name 'myLog.out' -a -size +0c \) |[ `wc -c` != 0 ] && mail user@domain < myLog.out

For extra points, how to set find statement to a variable and then reuse variable for redirecting to mail?
James R. Ferguson
Acclaimed Contributor

Re: conditional mail on find?

Hi Anna:

By example, consider this:

# DIR=/tmp
# NAME=myLog.out
# SIZE=500
# find ${DIR} \( -name ${NAME} -a -size +${SIZE}c \)

Regards!

...JRF...

Yogeeraj_1
Honored Contributor

Re: conditional mail on find?

hi,

i usually do it this way:
#=============================
#!/bin/sh
emailadd1=yogeeraj@mymailserver.mu
DIR=/tmp
NAME=105.sh
SIZE=5
m=`find ${DIR} \( -name ${NAME} -a -size +${SIZE}c \)`

/usr/bin/uuencode $m "logfile_needed.log"|mailx -m -s "The is the file you need - `date`" $emailadd1

#=============================

Hope this helps!
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Anna Fong
Advisor

Re: conditional mail on find?

James,

Thanks again for the example. This is what I ended up with (all on one line) --

# FPATH=/path/to; FNAME=myLog.out; find ${FPATH} \( -name ${FNAME} -a -size +0c \) |[ `wc -c` != 0 ] && > mail user@domain < "${FPATH}/${FNAME}"

Anna Fong
Advisor

Re: conditional mail on find?

Yogeeraj,

Thanks for your example. Didn't try it out because I ended up doing this --

# FPATH=/path/to; FNAME=myLog.out; find ${FPATH} \( -name ${FNAME} -a -size +0c \) |[ `wc -c` != 0 ] && > mail user@domain < "${FPATH}/${FNAME}"