Unix 11i v2 Script to detect new files within directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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









share|improve this question
























  • 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
















0















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









share|improve this question
























  • 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














0












0








0


1






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









share|improve this question
















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






cron






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 15 '14 at 12:16







jes516

















asked Jul 13 '14 at 21:01









jes516jes516

3331721




3331721












  • 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


















  • 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

















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





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





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






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






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






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











2 Answers
2






active

oldest

votes


















0














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
#!/bin/bash
echo $1 >> /path/to/logfile
EOF
chmod +x /path/to/mylogger.sh


Thats it.






share|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


















0














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.



#!/usr/bin/sh

previous=/tmp/previous
new=/tmp/newfiles
DIR=/tmp/aa

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

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

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

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

ls -rt | while read file
do
if [[ "$file" -nt "$ref" ]]
then
echo "$file"
fi
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





share|improve this answer
























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f144367%2funix-11i-v2-script-to-detect-new-files-within-directory%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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
    #!/bin/bash
    echo $1 >> /path/to/logfile
    EOF
    chmod +x /path/to/mylogger.sh


    Thats it.






    share|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















    0














    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
    #!/bin/bash
    echo $1 >> /path/to/logfile
    EOF
    chmod +x /path/to/mylogger.sh


    Thats it.






    share|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













    0












    0








    0







    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
    #!/bin/bash
    echo $1 >> /path/to/logfile
    EOF
    chmod +x /path/to/mylogger.sh


    Thats it.






    share|improve this answer













    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
    #!/bin/bash
    echo $1 >> /path/to/logfile
    EOF
    chmod +x /path/to/mylogger.sh


    Thats it.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 13 '14 at 22:44









    Dani_lDani_l

    3,185929




    3,185929







    • 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












    • 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







    2




    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





    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





    hi user60039, you are correct, I am using HP-UX ...

    – jes516
    Jul 14 '14 at 0:51













    0














    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.



    #!/usr/bin/sh

    previous=/tmp/previous
    new=/tmp/newfiles
    DIR=/tmp/aa

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

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

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

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

    ls -rt | while read file
    do
    if [[ "$file" -nt "$ref" ]]
    then
    echo "$file"
    fi
    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





    share|improve this answer





























      0














      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.



      #!/usr/bin/sh

      previous=/tmp/previous
      new=/tmp/newfiles
      DIR=/tmp/aa

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

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

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

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

      ls -rt | while read file
      do
      if [[ "$file" -nt "$ref" ]]
      then
      echo "$file"
      fi
      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





      share|improve this answer



























        0












        0








        0







        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.



        #!/usr/bin/sh

        previous=/tmp/previous
        new=/tmp/newfiles
        DIR=/tmp/aa

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

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

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

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

        ls -rt | while read file
        do
        if [[ "$file" -nt "$ref" ]]
        then
        echo "$file"
        fi
        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





        share|improve this answer















        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.



        #!/usr/bin/sh

        previous=/tmp/previous
        new=/tmp/newfiles
        DIR=/tmp/aa

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

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

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

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

        ls -rt | while read file
        do
        if [[ "$file" -nt "$ref" ]]
        then
        echo "$file"
        fi
        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 15 '14 at 10:20

























        answered Jul 14 '14 at 11:16









        EmmanuelEmmanuel

        3,05911120




        3,05911120



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f144367%2funix-11i-v2-script-to-detect-new-files-within-directory%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay