I was hoping someone can point me in the right direction. I would like to create a script to check for new files (files with today's date) within a directory and then print the results to a text file. Could I use cron or bash?

edit: Updated original thread. I tried the following on my Linux Mint VM and I got it to work. The only problem is:

1) when i create a new file (using "sudo vi newtextfile.txt" and then ":w") i receive the notification email 4 times.

2) am i triggering multiple IN_CREATE events by creating a new file using the method i mentioned above?

/xyz/jon >>>
total 12
786436 drwxr-xr-x 2 root root 4096 Jul 13 20:43 .
786434 drwxr-xr-x 4 root root 4096 Jul 13 19:45 ..
786441 -rwxr-xr-x 1 root root 48 Jul 13 20:32 mylogger.sh
786437 -rw-r--r-- 1 root root 0 Jul 13 20:22 mylogtoday.txt

"incron.table-jonincron" >>>
/xyz/client IN_CREATE /xyz/jon/mylogger.sh

/xyz/jon/mylogger.sh >>>
sudo mail -s "testing script" emailaddress@domain.com
  • Well bash is a shell scripting language and interpreter; that's as good a place as any to start with writing a shell script. – HalosGhost Jul 13 '14 at 21:31
  • So you want to get a list of all files modified since midnight? If the directory has directories in it, do you want to look through them, too? – Mark Plotnick Jul 15 '14 at 8:29
  • no, i want to check for new files within a directory /xyz/client, if there is a new file (file was created today), then either send an email notification (as i did above) or print a list of files within /xyz/client as a text file to a separate directory /xyz/jon – jes516 Jul 15 '14 at 12:23
  • OK. By "created today", do you mean "did not exist prior to midnight but exists now"? Unix (except on Mac OS X and on ext4 filesystems on Linux) doesn't keep track of when a file was created. For your HP-UX system, it sounds like what you want is to create a list of files at midnight, then create another list of files later in the day, and compare the two lists. Does that sound right? – Mark Plotnick Jul 15 '14 at 14:18
  • yes, if we cannot monitor/track when new files are created within that directory (/xyz/client), then that sounds like the way to go (compare: yesterdays file prior to midnight vs todays file after midnight) and send an email notification when there are differences between the two files. if you can help me out with this, i would highly appreciate it. We are on HP-UX B.11.23 U 9000/800 if it helps... and i am in the process of setting up mailx for use in command line for script.. – jes516 Jul 15 '14 at 19:46

Do you want something that monitors a directory? incron can be quite handy for that:
1. Install incron from a repo
2. Enable incrond:

sudo systemctl enable incrond.service
sudo systemctl start incrond.service

3.Create appropriate incrontab:

incrontab -e

and add as content:

/path/to/watch IN_CREATE /path/to/mylogger.sh $@/$#

4.Create the logger script:

cat <<EOF>> /path/to/mylogger.sh
echo $1 >> /path/to/logfile
chmod +x /path/to/mylogger.sh

Thats it.

| improve this answer | |
  • 2
    Given that the parent is talking about "Unix 11i v2", I assume that he is talking of HP-UX, which doesn't have inotify (and thus incron) nor systemd, so your answer wouldn't work for him. Unless you're suggesting he should change his OS... – user60039 Jul 14 '14 at 0:03
  • hi user60039, you are correct, I am using HP-UX ... – jes516 Jul 14 '14 at 0:51

Hello I was hopping someone with better skill could answer. HP-UX has quite an old System V toolbox so you will not have the recent GNU tools.

HP-UX posix shell, which is /usr/bin/sh is very close to ksh93. So you can compare the age of two files with [[ file1 -nt file2 ]] wich returns true if file1 is newer than file2. Don't use /usr/bin/ksh on HP-UX it is a very old standard korn-shell (perhaps 1982).

Here is a simple script that you can run periodically. It should be hardened better to support files with specials characters in the names and other possible errors but I'm not good at that.
The files being watched are in DIR, the new files list is /tmp/newfile. The list is created while comparing all the file in DIR to the youngest file found during the last run so it supports new files being created while it runs. The script does not support multiple execution at the same time, you may have to implement locks or to space each execution with enough time.



if [[ ! -s $new ]]
    mv "$new" "$previous"

if [[ ! -f "$previous" ]]
    ls -rt "$DIR" > "$new"

ref=$(tail -1 "$previous")

if ! cd "$DIR"
    echo cannot cd "$DIR"
    exit 1

ls -rt | while read file
        if [[ "$file" -nt "$ref" ]]
             echo "$file"
done > "$new"

You could run it in cron, for instance :

every five minutes (note that hp-ux crontab do not support *5 syntax)

0,5,10,15,20,25,30,35,40,45,55 * * * * /myscript.sh >>/var/adm/myscript.log 2>&1

every hour

0 * * * * /myscript.sh >>/var/adm/myscript.log 2>&1

once a day

0 0 * * * /myscript.sh >>/var/adm/myscript.log 2>&1
| improve this answer | |

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.