Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to unix scripting. I have to do something like :

In a folder /x everyday 10Pm and next day 3 AM, 2 files are placed. I have to write a script which will check the folder /x and confirm if the files are there or not and send mails accordingly everyday.

file formats:

  • ValuationIR**02092013**_0326 (FileName pattern: ValuationIR<date>_<time>.csv )
  • SnapshotIR**02082013**_2238 (FileName pattern: SnapshotIR<date>_<time>.csv )

Could someone help on how can I achieve the same, and which all commands would be required.

share|improve this question

If you simply need to check if the two files exist and send an email if they do not, something like this should do nicely:

test -f file1 -a -f file2 || ( echo "Either file1 or file2 doesn't exist" | sendmail ... )

The paranthesis are not strictly necessary, but I'm including them for clarity. You can also use bash functions to make this a bit more readable and easily expandable, but that won't be shell-agnostic (though most shells have ways to do the same thing).

send_mail() {
    echo "message text" | sendmail ...
test -f file1 -a -f file2 || send_mail

This can be further expanded on if necessary, but the above should get you started.

When the script itself works as intended, you can add it to your user crontab to run it nightly at a specified time.

share|improve this answer
The files have to be searched with the pattern (starts with <SnapshotIR> + contains <previous date>), as there would be earlier dated files as well. – Swagatika Feb 11 '13 at 12:08
So make file1 and file2 match your required filenames. You have to do some of the work yourself. – Jason Tan Feb 11 '13 at 12:32
And/or add checks to see if the files' content match what you expect. Just expand on the part before || to do whatever checks you want. – Michael Kjörling Feb 11 '13 at 12:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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