I want to select latest error(timestamp).log file from some directory and send it via email as attachment. What I am trying is:

Filename: abc.sh

echo 'An error occured' | mutt -s "Logs" -a '/xx/xx/logs/xx/*.log(.om[1])' -e 'my_hdr From:xx@xx.com' -- xx@xx.com
  • What does (.om[1]) signify? Is this part of the file name? – John1024 Jul 7 '15 at 20:47
  • I found that here and was trying to use that in my code : stackoverflow.com/questions/1015678/… – Brinda Jul 7 '15 at 20:53
  • Is there any other way to attach latest logs in email? – Brinda Jul 7 '15 at 20:54
  • @John1024 - that's a zsh glob qualifier. It sorts (o) plain files (.) by the time of the last modification (m) and selects only the first ([1]) one. – don_crissti Jul 7 '15 at 21:15
  • 1
    Yes I agree zsh has some really nice features. Wish it worked for me but unfortunately I cannot used zsh for my code. – Brinda Jul 7 '15 at 21:25

Naive Solution

I will assume that your logs have file names which match the glob /xx/xx/logs/xx/*.log and that you want to send mail to somebody@host.com

newest=$(ls -rt /xx/xx/logs/xx/*.log | tail -n 1)
echo 'An error occured' | mutt somebody@host.com -s "Logs" -a "$newest"

This approach works if the files have well-behaved names. In general, though, parsing the output of ls is unreliable.

More Reliable Solution

This avoids using ls and will be safe for any and all file names:

inode=$(find /xx/xx/logs/xx/ -maxdepth 1 -type f -iname '*.log' -printf '%T@ %i\n' | sort -rn | awk '{print $2;exit;}')
newest=$(find /xx/xx/logs/xx/ -maxdepth 1 -inum "$inode")
echo 'An error occured' | mutt somebody@host.com -s "Logs" -a "$newest"

Testing to see which file is selected

To see which file is the newest, without emailing it, run:

inode=$(find /xx/xx/logs/xx/ -maxdepth 1 -type f -iname '*.log' -printf '%T@ %i\n' | sort -rn | awk '{print $2;exit;}')
newest=$(find /xx/xx/logs/xx/ -maxdepth 1 -inum "$inode")
echo "newest file is $newest"
  • the log file name changes and every time timestamp is added to the log file. – Brinda Jul 7 '15 at 21:24
  • "the log file name changes" OK. This solution finds the most recent file in the xx/xx/logs/xx/ directory that has a name ending in .log. Do you need something else? "every time timestamp is added to the log file" The contents of the log file will all be sent in the email. – John1024 Jul 7 '15 at 21:28
  • When I tried using this code I got this error: find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-iname /xx/xx/logs/xx/*.log'' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ /xx/xx/logs/xx/*.log''. find: missing argument to `-inum' Can't stat (null): Bad address (null): unable to attach file. – Brinda Jul 7 '15 at 21:29
  • Oops, sorry! Answer corrected. – John1024 Jul 7 '15 at 21:37
  • Hi, But it still gives me this error: Can't stat (null): Bad address (null): unable to attach file. – Brinda Jul 7 '15 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.