-1

I would like to count the number of files in single folder and, if the number of files exceeds an certain number (eg. 5000), an email should be sent.

The commands are like this, but I'd like to have it in one single line (using later for cron job):

count=$(find /dir1/dir2/dir3 -name "*.jpg" | wc -l) 
if [ $count -gt 5000 ]
then
    <command to send email>
else
    <do nothing>
fi
eush77
  • 3,870
  • 1
  • 23
  • 30

1 Answers1

1

You can put it one line separating commands with a semi column and using the special quotes (``) to retrieve the value of the output (wc -l in this case):

if [ `find /dir1/dir2/dir3 -name "*.jpg" | wc -l` -gt 5000 ]; then <command to send email>; else <do nothing>; fi

and if you "do nothing" really is nothing, then you can just leave it out:

if [ `find /dir1/dir2/dir3 -name "*.jpg" | wc -l` -gt 5000 ]; then <command to send email>; fi

To send a mail, you can use mail, for example with:

echo "this is the body" | mail -s "this is the subject" "to@address"

check the different options in this question: How to send email from Terminal?.

Community
  • 1
  • 1
agold
  • 6,140
  • 9
  • 38
  • 54
  • Thanks for your solution I've tried already with ";" between the commands, but this hasn't worked. Maybe I used wrong parameters or not the correct positions for the characters ";". I will try this again... – kickwin2pampas Nov 30 '15 at 17:54