-   Linux - Newbie (
-   -   Can I use a wildcard when using the mailx command? (

00mpa 02-25-2013 10:54 AM

Can I use a wildcard when using the mailx command?
Hi all,

I am trying to email a text file using the mailx command. I am doing the following:


mailx -s 'Report' emailaddress < /directory_name/filename.out

The problem comes about in the fact that I will not know the exact name of the file being emailed so I wanted to use a wildcard since it changes several times per day, but I know that it will have a .out extension. Just a random FYI, I am not worried about sending the wrong file because the .out file will be the only one in the directory. Anyway, I tried doing the following, but when I try running the script below, I get an error saying "/directory_name/*.out: cannot open"


mailx -s 'Report' emailaddress < /directory_name/*.out

Can anyone help?


shivaa 02-25-2013 11:07 AM

Can you once try:

~$ mailx -s 'Report' <(cat /directory_name/*.out)
But are you sure that there will be only one file with extension .out, irrespective of filename?

00mpa 02-25-2013 11:15 AM


Yes, I'm positive that there will only be one .out file. The reason why I don't know the name of the file is because the file is generated several times per day, but during its generation process, it deletes all .out files before creating the new updated .out file.

00mpa 02-25-2013 11:23 AM

Also, I tried running both versions that you suggested and I get a syntax error saying '(' unexpected.

shivaa 02-25-2013 11:39 AM

It's bash specific process substitution. So either change shell to bash and then invoke it:

bash~$ bash
bash~$ mailx -s 'Report' <(cat /directory_name/*.out)

Or, you can try:

bash~$ find /directory_name -name "*.out" -print | xargs mailx -s 'Report'
bash~$ mailx -s 'Report' <(find /directory_name -name "*.out" -print)

00mpa 02-25-2013 12:19 PM

If I understood your advice correctly, I tried the following three things with different results for each:

First Option:


mailx -s 'Report' <(cat /directory_name/*.out)

And I get a "cannot open /directory_name/*.out" error. Also, no email is sent.

Second Option:

find /directory_name -name "*.out" -print | xargs mailx -s 'Report'
mailx -s 'Report' <(find /directory_name -name "*.out" -print)

I get a blank prompt so when I do Ctrl+C so that the blank prompt goes away, it sends the email, but it's blank.

Third Option:
find /directory_name -name "*.out" -print | xargs mailx -s 'Report'

Sends a blank email.

I'm sorry this may seem super basic, but I'm simply trying to learn how to do this with little background knowledge in the subject.


chrism01 02-25-2013 07:06 PM


cat *.out|mailx -s 'Report'
For more general process output eg

(uname -a) |mailx -s 'Report'

shivaa 02-25-2013 07:30 PM

This is working fine:

~$ mailx -s 'Report' < $(find /directory_name -name *.out -print)
Else you can even use:

~$ mailx -s 'Report' < *.out
But there should be only one file with .out extension, else you will get error like:

-bash: *.out: ambiguous redirect

All times are GMT -5. The time now is 06:16 AM.