Fedora Linux Support Community & Resources Center
  #1  
Old 1st February 2012, 02:26 PM
mathboy314 Offline
Registered User
 
Join Date: Nov 2005
Posts: 30
windows_7ie
script to email last 10 files

Hello all,

I'm trying to create a shell script to attach the last 10 files in a certain directory to an email message and email it to myself.

I've googled quite a bit on this and can't seem to get it right.

Here is what I have so far:

mutt -s "last 10 files" -a /directory/$(ls /directory | tail -1) -- my@email.com < message.txt

Except this will only send me the most recent file only (hence tail -1)...I've tried tail -2..etc...but that fails.

Any ideas?

I realize I could zip them and send it but it would be so much better to just attach the files in case I'm on a device that can't unzip files.
Reply With Quote
  #2  
Old 1st February 2012, 02:34 PM
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Confoederatio Helvetica (Swissh)
Age: 34
Posts: 3,487
linuxfedorafirefox
Re: script to email last 10 files

Sounds like homework, so..

You could sort them by date (ls --help), parse the list adding those 10 to another list, and then use that new list to add the files to the email.
__________________
Laptop: Toshiba satellite p50-a-11 CPU: Intel i7 8*2400 MHz GPU: GeForce GT 745M RAM: 8192 MB Distro: Fedora (Rawhide) DE: Awesome
Text User Interface (TUI) // Windows 8+ & Fedora 20+ Dualboot
Reply With Quote
  #3  
Old 1st February 2012, 02:37 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,866
linuxfedorafirefox
Re: script to email last 10 files

I would have thought using "... -a `ls /directory | tail -10` -- ..." would have worked. Using "/directory/$(..." should have caused the first file to fail as being "/directory/directory/<name>" problem. Another possibility is that the output might not have been a single line (tail -10 would generate multiple end-of-line characters that might not be handled properly... The `...` syntax is specifically to remove that).

You also might consider using `find /directory/* -prune | ...` useful as that includes the "/directory" path on each file listed.

Last edited by jpollard; 1st February 2012 at 02:44 PM.
Reply With Quote
  #4  
Old 1st February 2012, 04:22 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,685
linuxfedorafirefox
Re: script to email last 10 files

I believe you have to specify separate "-a file" options for each attachment:
Code:
#!/bin/bash
DIR=/some/directory
ATTACH=""
IFS="$(echo -e "\n\r")"
for f in $(/bin/ls -tr ${DIR}|tail -10)
do
   ATTACH="${ATTACH} -a ${DIR}/${f}"
done
unset IFS
mutt -s "last 10 files" ${ATTACH} -- me@email.com < message.txt
I don't have mutt installed but the above script worked with mutt replaced by the mailx command. I'll leave it to you to figure out how to handle file names with spaces in them.
Reply With Quote
  #5  
Old 1st February 2012, 05:02 PM
mathboy314 Offline
Registered User
 
Join Date: Nov 2005
Posts: 30
windows_7ie
Re: script to email last 10 files

Brilliant.

Thanks RupertPupkin, works great with mutt too.
Reply With Quote
  #6  
Old 15th February 2012, 01:51 AM
robertdaleweir Offline
Registered User
 
Join Date: Oct 2005
Location: British Columbia, Canada
Posts: 421
linuxfirefox
Re: script to email last 10 files

Quote:
Originally Posted by RupertPupkin View Post
I believe you have to specify separate "-a file" options for each attachment:
Code:
#!/bin/bash
DIR=/some/directory
ATTACH=""
IFS="$(echo -e "\n\r")"
for f in $(/bin/ls -tr ${DIR}|tail -10)
do
   ATTACH="${ATTACH} -a ${DIR}/${f}"
done
unset IFS
mutt -s "last 10 files" ${ATTACH} -- me@email.com < message.txt
I don't have mutt installed but the above script worked with mutt replaced by the mailx command. I'll leave it to you to figure out how to handle file names with spaces in them.
Hi RupertPupkin
I was searching for articles regarding mailx and came upon this one. Since it is not too old, I thought I might ask for some help rather than start a new thread. I will start a new thread if you think I should.
I have been trying to use mailx from a user account. It seems that the mail is not getting through. Some of the last entries in the ,maillog file are set out below.
Quote:
Feb 14 12:38:55 mauve sendmail[11139]: q1EKcsem011139: to=robweir@telus.net, ctladdr=robert (1000/1000), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30232, relay=[127.0.0.1] [127.0.0
.1], dsn=2.0.0, stat=Sent (q1EKcss9011140 Message accepted for delivery)
Feb 14 12:38:55 mauve sendmail[11142]: q1EKcss9011140: to=<robweir@telus.net>, ctladdr=<robert@mauve.localdomain> (1000/1000), delay=00:00:01, xdelay=00:00:00, mailer=esmtp, pri=120483, rel
ay=mx.svc.telus.net. [199.185.220.200], dsn=5.0.0, stat=Service unavailable
None of these messages got through. I have a .forward file in the home directory with my email address in it.
Code:
 robweir@telus.net
I am unfamiliar with the mail system on Linux and was wondering if you could help. Thanks...
__________________
Robert :)
Kernel 3.9.10-100.fc17.x86_64 GNOME 3.4.2
CPU AMD 955 Quad Core
MoBo Gigabyte MA785GM-UD2H-AM3 MicroATX
Memory 8 Gigs OCZ DDR3-1333 PC-10666
Reply With Quote
  #7  
Old 15th February 2012, 04:53 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,685
linuxfedorafirefox
Re: script to email last 10 files

Robert, see this post: http://forums.fedoraforum.org/showpo...99&postcount=2

Adjust the settings in the ~/.mailrc file you create to fit your setup.
You can use mailx instead of nail in that last command (in fact now /usr/bin/nail is a link to /bin/mailx).
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #8  
Old 15th February 2012, 03:39 PM
robertdaleweir Offline
Registered User
 
Join Date: Oct 2005
Location: British Columbia, Canada
Posts: 421
linuxfirefox
Re: script to email last 10 files

Quote:
Originally Posted by RupertPupkin View Post
Robert, see this post: http://forums.fedoraforum.org/showpo...99&postcount=2

Adjust the settings in the ~/.mailrc file you create to fit your setup.
You can use mailx instead of nail in that last command (in fact now /usr/bin/nail is a link to /bin/mailx).
Hi Rupert
Thanks so much, I will give that a try. Cheers...

---------- Post added 15th February 2012 at 07:39 AM ---------- Previous post was 14th February 2012 at 10:58 PM ----------

Quote:
Originally Posted by RupertPupkin View Post
Robert, see this post: http://forums.fedoraforum.org/showpo...99&postcount=2

Adjust the settings in the ~/.mailrc file you create to fit your setup.
You can use mailx instead of nail in that last command (in fact now /usr/bin/nail is a link to /bin/mailx).
Hi
I looked at the URL and I have a couple of questions about the entries in the ./.mailrc . How do I find my my.smtp.server? I ran
Code:
systemctl -a
and found that the sendmail.service is running, is that likely to be
HTML Code:
my.smtp.server
. How does one specifically find out what SMTP server is active on my machine and what port is used? Is port 465 always the appropriate port? If this is the MTA being used, how does one specifically refer to it in the ./.mailrc?
I am a bit of a noob on this mail thing.
Thanks
__________________
Robert :)
Kernel 3.9.10-100.fc17.x86_64 GNOME 3.4.2
CPU AMD 955 Quad Core
MoBo Gigabyte MA785GM-UD2H-AM3 MicroATX
Memory 8 Gigs OCZ DDR3-1333 PC-10666
Reply With Quote
  #9  
Old 15th February 2012, 05:28 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,685
linuxfedorafirefox
Re: script to email last 10 files

Quote:
Originally Posted by robertdaleweir View Post
How do I find my my.smtp.server?
That's something that you find out from your internet service provider. For example, my ISP is Comcast, and their SMTP server's hostname is smtp.comcast.net. They use secure connections (as most ISPs do nowadays) on port 465, so the smtp variable in my ~/.mailrc is set to "smtps://smtp.comcast.net:465". My full ~/.mailrc looks like this
Code:
set smtp=smtps://smtp.comcast.net:465
set smtp-auth=login
set smtp-auth-user=MYUSERID
set smtp-auth-password=MYPASSWORD
set ssl-verify=ignore
set nss-config-dir=~/mailcerts
where of course I'm not going to reveal the actual values of MYUSERID and MYPASSWORD here.
I can then send mail to somebody@somewhere.com like this
Code:
mailx -s "Some subject" -r MYUSERID@comcast.net somebody@somewhere.com < message.txt
where message.txt is a plain text file containing the message I want to send.

So you need to get the SMTP info from your ISP and use the username and password for the account you have with them. Since the ~/.mailrc file contains a password you should protect it so that only you (or root) can read it:
Code:
chmod 600 ~/.mailrc
The root account can still read that, but that's one of the the drawbacks of doing this via command-line without interaction. You can avoid putting the password in your ~/.mailrc and then get prompted to enter it (which could also be a problem) when running the mailx command. Read the mailx man page for the details.
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #10  
Old 1st February 2012, 05:50 PM
weitjong Online
Registered User
 
Join Date: Oct 2006
Location: Singapore, 新加坡
Posts: 946
linuxfirefox
Re: script to email last 10 files

Alternatively, you can temporary switch to zsh in order to use its file globbing feature.
Code:
$ zsh
% mutt -s "last 10 files" -a /directory/*(.om[1,10]) -- my@email.com < message.txt
% exit
$
__________________
YaoWT - Leave no window unbroken ♪ (^。^) 
Reply With Quote
  #11  
Old 15th February 2012, 09:29 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,866
linuxfedorafirefox
Re: script to email last 10 files

A local .forward file is only used if you operate a local mail server.

These can be handy... as the .forward file can do several things
1. inform the incoming mail handler where to forward the mail
2. allow for a process to filter the incoming mail (one way to handle spam, or to identify high priority mail...

But it depends on you running a local mail server. Most people don't, though root can still send local mail, and that can be processed.
Reply With Quote
  #12  
Old 15th February 2012, 09:56 PM
robertdaleweir Offline
Registered User
 
Join Date: Oct 2005
Location: British Columbia, Canada
Posts: 421
linuxfirefox
Re: script to email last 10 files

Quote:
Originally Posted by jpollard View Post
A local .forward file is only used if you operate a local mail server.

These can be handy... as the .forward file can do several things
1. inform the incoming mail handler where to forward the mail
2. allow for a process to filter the incoming mail (one way to handle spam, or to identify high priority mail...

But it depends on you running a local mail server. Most people don't, though root can still send local mail, and that can be processed.
Hi jpollard
I appreciate your help. Great information. One question on your last comment. Is it possible to send mail as root, which can be sent to an external (to my system) Email address? If so should one have a .forward under /root or is it necessary?
Thanks...
__________________
Robert :)
Kernel 3.9.10-100.fc17.x86_64 GNOME 3.4.2
CPU AMD 955 Quad Core
MoBo Gigabyte MA785GM-UD2H-AM3 MicroATX
Memory 8 Gigs OCZ DDR3-1333 PC-10666
Reply With Quote
  #13  
Old 16th February 2012, 12:25 AM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,866
linuxfedorafirefox
Re: script to email last 10 files

There are two ways to do it -

The .forward is aimed at user modification and can be quite flexible.

The other method is to define an alias for root that has the target mail.

Some/many ISPs block incoming port 25 connections, but don't block outgoing. 25 is the default mail port. Some ISPs use a different port just to avoid some external traffic, even though it really doesn't matter. Some require connections to have DNS authentication or other host authentication. This is not password type authentication, they just want to confirm that a remote mail server may be valid. These may cause errors, though you should get error messages about it.

You can always test by using utilities like mailx which make a TCP connection to the remote server (over port 25 by default) and send a test message. You can read the mailx manpage on how to specify a nonstandard port number.
Reply With Quote
  #14  
Old 17th February 2012, 04:54 PM
robertdaleweir Offline
Registered User
 
Join Date: Oct 2005
Location: British Columbia, Canada
Posts: 421
linuxfirefox
Re: Update

Quote:
Originally Posted by jpollard View Post
There are two ways to do it -

The .forward is aimed at user modification and can be quite flexible.

The other method is to define an alias for root that has the target mail.

Some/many ISPs block incoming port 25 connections, but don't block outgoing. 25 is the default mail port. Some ISPs use a different port just to avoid some external traffic, even though it really doesn't matter. Some require connections to have DNS authentication or other host authentication. This is not password type authentication, they just want to confirm that a remote mail server may be valid. These may cause errors, though you should get error messages about it.

You can always test by using utilities like mailx which make a TCP connection to the remote server (over port 25 by default) and send a test message. You can read the mailx manpage on how to specify a nonstandard port number.
Hi jpollard
I have found out that my ISP, a local Telco, is using a Spam filter thingy call 'spamhaus' . They are a legitimate service which filters E-Mail that has been generated from "inappropriate IP' (their words) addresses, which are not Certified Mail Server IP addresses (and I suspect I did not have the correct login info in my script). My ip 206.116.39.nnn is not a generally accepted Mail Server IP range.
I suspect they also want to ensure, that as a Home User, one does not run any servers open directly to the public, in particular Mail. Anyway after you guys helped me fix my local problem, I hit this one. I think I can fix it with the appropriate Authentication UID and password.
Just thought I would post anyway in case someone else has a similar problem. Cheers...
__________________
Robert :)
Kernel 3.9.10-100.fc17.x86_64 GNOME 3.4.2
CPU AMD 955 Quad Core
MoBo Gigabyte MA785GM-UD2H-AM3 MicroATX
Memory 8 Gigs OCZ DDR3-1333 PC-10666
Reply With Quote
  #15  
Old 16th February 2012, 02:50 AM
robertdaleweir Offline
Registered User
 
Join Date: Oct 2005
Location: British Columbia, Canada
Posts: 421
linuxfirefox
Re: script to email last 10 files

Hi jpollard
Thanks again. That gives me a good start on Mail and its set up. You have a great day...
__________________
Robert :)
Kernel 3.9.10-100.fc17.x86_64 GNOME 3.4.2
CPU AMD 955 Quad Core
MoBo Gigabyte MA785GM-UD2H-AM3 MicroATX
Memory 8 Gigs OCZ DDR3-1333 PC-10666
Reply With Quote
Reply

Tags
email, files, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
email via bash script lightman47 Using Fedora 4 10th March 2011 09:09 PM
Send an email from a bash script joe.pelayo Using Fedora 11 17th August 2009 10:35 PM
Email gathering script mh10190 Programming & Packaging 2 14th March 2006 12:44 PM
Email inc Attachment from script sc00by Using Fedora 2 13th August 2005 05:50 AM
email script ieuuk Using Fedora 1 20th June 2005 01:42 AM


Current GMT-time: 00:34 (Saturday, 25-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Lathi Travel Photos - Kanoya - Kaleke Mandi