LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2009, 03:33 AM   #1
cmadiam82
LQ Newbie
 
Registered: Nov 2008
Posts: 4

Rep: Reputation: 0
Shell script to monitor files created on a folder


Hi All Linux Gurus!!!

Can someone please help me on how can i create a script that will monitor file creation on a single folder and sending the newly created file on a separate folder? Only the new created file must be transffered or copied to the other folder. The old ones remains.

I urgently need this for production deployment.

Thanks in advance to all.

You kind help will be much appreciated.

Regards,
cmadiam
 
Old 08-27-2009, 07:55 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Check out iNotify..

http://en.wikipedia.org/wiki/Inotify

Down towards the bottom are some programming APIs for iNotify... I like incron and Pyinotify.
 
Old 08-27-2009, 11:23 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by cmadiam82
Can someone please help me on how can i create a script that will monitor file creation on a single folder and sending the newly created file on a separate folder? Only the new created file must be transffered or copied to the other folder. The old ones remains.
If I'm understanding, this may also be a good candidate for rsync(1).
 
Old 08-27-2009, 01:37 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
while sleep 10;do
  cd source
  find . | cpio -pd destination
done
 
Old 08-27-2009, 03:26 PM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
OK, bigearsbilly's method will be way I/O intensive. Try this instead:

If they are on the same partition:

Code:
#!/bin/sh
SOURCEDIR='/path/to/source/dir'
STORDIR='/path/to/storeage/dir'

while sleep 10 ; do
        for FILE in `ls $SOURCEDIR/* | awk -F/ '{print $NF}'` ; do
                if [ ! -e $STORDIR/$FILE ] ; then
                        ln /tmp/$FILE $STORDIR/$FILE
                fi
        done
done
If they are on different partitions then run that and then the following as often as you need to to not fill up your drive (in that case, /path/to/storage/dir is a temporary storage location):
Code:
rsync -a /path/to/storeage/dir/ /path/to/real/storage ; rm /path/to/storage/dir/*
HTH

Forrest

Last edited by forrestt; 08-27-2009 at 03:41 PM. Reason: forgot the sleep while statement & then I needed to spell SOURCEDIR the same twice.
 
Old 08-27-2009, 04:22 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
hmm!
not sure if that's true.
cpio -pd
only copies newer files.

it wasn't a serious offer, just a pointer.

how about a cron job?

Last edited by bigearsbilly; 08-27-2009 at 04:24 PM.
 
Old 08-27-2009, 04:27 PM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
OK, not from my interpretation of the man page. However, ln doesn't copy anything, it just makes a new inode that points to the already existing file.

Forrest

p.s. I already run that script from a cron job (without the sleep 10) every minute so I can monitor my son's activities on the net.

p.p.s. Wow, I'm a senior member now!!! Woot!!!

p.p.p.s. I guess I need to change my sig.

Last edited by forrestt; 08-28-2009 at 08:51 AM.
 
Old 08-28-2009, 12:26 AM   #8
cmadiam82
LQ Newbie
 
Registered: Nov 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Sir, how about if the destination is on the other server? this scenario will apply on our servers.


Quote:
Originally Posted by forrestt View Post
OK, bigearsbilly's method will be way I/O intensive. Try this instead:

If they are on the same partition:

Code:
#!/bin/sh
SOURCEDIR='/path/to/source/dir'
STORDIR='/path/to/storeage/dir'

while sleep 10 ; do
        for FILE in `ls $SOURCEDIR/* | awk -F/ '{print $NF}'` ; do
                if [ ! -e $STORDIR/$FILE ] ; then
                        ln /tmp/$FILE $STORDIR/$FILE
                fi
        done
done
If they are on different partitions then run that and then the following as often as you need to to not fill up your drive (in that case, /path/to/storage/dir is a temporary storage location):
Code:
rsync -a /path/to/storeage/dir/ /path/to/real/storage ; rm /path/to/storage/dir/*
HTH

Forrest
 
Old 08-28-2009, 08:40 AM   #9
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Change the rsync command to:

Code:
rsync -ave ssh monitoredsystem:/path/to/storeage/dir/ /path/to/real/storage
ssh someuser@monitoredsystem rm /path/to/storage/dir/\*
That should do it, but you will still want to test it since this is a production system.

HTH

Forrest

Last edited by forrestt; 08-28-2009 at 08:50 AM. Reason: forgot to excape the * in the rm command
 
Old 05-11-2013, 12:59 AM   #10
rnldpj
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
Script to monitor file creation in linux

Check out the script mentioned in the url pasted below. It uses inotifywait to recursively monitor and record file creation events.

http://jackal777.wordpress.com/2013/...-documentroot/
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Applying default permissions for newly created files within a specific folder mattydee Linux - Desktop 29 10-30-2016 09:55 PM
Sort files under a folder by created date Kunsheng Programming 3 05-11-2009 04:58 AM
Automatically set permissions of new files created within a specific folder Lorian Linux - Desktop 2 03-03-2007 03:17 PM
Need shell script to set passwords for already created users naren_0101bits Programming 2 08-28-2005 02:02 PM
I have 8 character file name, no extension files being created in my dbaseIII+ folder bonzo Linux - General 2 01-22-2004 06:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration