Watch a directory for new files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Watch a directory for new files
# 8  
Old 11-02-2012
@os2mac

First, as Scott said, you didn't mentionned your distro.

Then, i also mentionned this alternative in case people have it on their distro ... since your point was "sharing the information" about "watching a directory for new file".

... And if we talk about what people "assume" , you assumed that the ".newer" file already exists which may not be the case at the first loop of your code ... Smilie

Code:
$ while [ 1 ];
> do
>         watchdir=/var/tmp
> newfile=$watchdir/.newer
>         find $watchdir -newer $newfile; 
> touch -a -m $newfile;
>         sleep 5;
> done
find: "/var/tmp/.newer": Aucun fichier ou dossier de ce type

Smilie
This User Gave Thanks to ctsgnb For This Post:
# 9  
Old 11-02-2012
Quote:
Originally Posted by ctsgnb
@os2mac

First, as Scott said, you didn't mentionned your distro.

Then, i also mentionned this alternative in case people have it on their distro ... since your point was "sharing the information" about "watching a directory for new file".

... And if we talk about what people "assume" , you assumed that the ".newer" file already exists which may not be the case at the first loop of your code ... Smilie

Code:
$ while [ 1 ];
> do
>         watchdir=/var/tmp
> newfile=$watchdir/.newer
>         find $watchdir -newer $newfile; 
> touch -a -m $newfile;
>         sleep 5;
> done
find: "/var/tmp/.newer": Aucun fichier ou dossier de ce type

Smilie
The irony, it burns us when we touches it..
# 10  
Old 11-05-2012
os2mac:

I applaud your desire to help others, but, without exaggerating, your solution is utterly unfit for deployment.

Quote:
Originally Posted by os2mac
Code:
#!/bin/bash

while [ 1 ];
do
        watchdir=/var/tmp
	newfile=$watchdir/.newer
        touch $newfile
        --> ONLY FILES CREATED DURING THIS WINDOW ARE SEEN <-- 
        find $watchdir -newer $newfile; 
	touch -a -m $newfile;
        sleep 5;
done

Each iteration of that loop will consume approximately 5 seconds, most of it sleeping while files that will never be detected are created.

If things that do not need to be in the loop are extracted, the situation improves significantly:
Code:
watchdir=/var/tmp
newfile=$watchdir/.newer
touch $newfile
while [ 1 ];
do
        find $watchdir -newer $newfile; 
        -->FILES CREATED DURING THIS WINDOW ARE LOST<-- 
	touch -a -m $newfile;
        sleep 5;
done

There is still a race condition between find and touch, but at least now it's only a tiny fraction of each iteration's wall clock run time.

This is much better, and perhaps it's sufficient for personal use, but if a file cannot go unreported, it's still inadequate.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 11  
Old 11-05-2012
Quote:
Originally Posted by alister
os2mac:

I applaud your desire to help others, but, without exaggerating, your solution is utterly unfit for deployment.



Each iteration of that loop will consume approximately 5 seconds, most of it sleeping while files that will never be detected are created.

If things that do not need to be in the loop are extracted, the situation improves significantly:
Code:
watchdir=/var/tmp
newfile=$watchdir/.newer
touch $newfile
while [ 1 ];
do
        find $watchdir -newer $newfile; 
        -->FILES CREATED DURING THIS WINDOW ARE LOST<-- 
    touch -a -m $newfile;
        sleep 5;
done

There is still a race condition between find and touch, but at least now it's only a tiny fraction of each iteration's wall clock run time.

This is much better, and perhaps it's sufficient for personal use, but if a file cannot go unreported, it's still inadequate.

Regards,
Alister
Thanks for the education Obi Wan, I will endevour to follow you're guidance in the future, master. What other foo can you impart upon a lowly Solaris Junkie?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

3. Shell Programming and Scripting

Watch a directory for files with a pecific pattern

I dont know if this possible. I need to watch a directory and if any file gets appeneded with a particular entry say, nologin.php, we should get output of that. (1 Reply)
Discussion started by: anil510
1 Replies

4. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

5. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

6. UNIX for Advanced & Expert Users

How to rsync or tar directory trees, with hidden directory, but without files?

I want to backup all the directory tress, including hidden directories, without copying any files. find . -type d gives the perfect list. When I tried tar, it won't work for me because it tars all the files. find . -type d | xargs tar -cvf a.tar So i tried rsync. On my own test box, the... (4 Replies)
Discussion started by: fld2007
4 Replies

7. Shell Programming and Scripting

KSH Script to watch log files for 2 strings

Hi all, How can I do a ksh script to watch a log file for 2 specific alarms that have one this string : "Connection Error" and the other one: "Failure in sending". I would like to watch for these two alarms in the log and then if each of them repeats for about 30 times to go kill 2 processes. ... (1 Reply)
Discussion started by: Pouchie1
1 Replies

8. UNIX for Advanced & Expert Users

Watch directory and move specific file extensions

Hi all, This is actually more for my lazyness then anything else, but I think others might find it useful to use as well. Basically this is what I am trying to achieve... In my ubuntu home dir under Downloads is where firefox saves everything by default, now I know that you can manually... (3 Replies)
Discussion started by: STOIE
3 Replies

9. UNIX for Dummies Questions & Answers

Which log files to watch for size?

I'm rusty with unix admin, and I was just informed by a peer in another city that I need to watch my unix log files for size, but she never told me which ones to watch - and of course, now she's gone for two weeks! I am using Sco OpenServer 5.5 (I believe), can anyone tip me off on the trouble log... (3 Replies)
Discussion started by: citygov
3 Replies

10. UNIX for Dummies Questions & Answers

script to watch changes on a directory

Hi, everybody I want to know hot to watch changes on a dir, for example if someone changes a file inside it, with an script. I've tried using md5sum and then diff, sadly with no success. I use md5sum for single files, but doesn't work for directories. The idea is take a snapshot with md5sum,... (4 Replies)
Discussion started by: piltrafa
4 Replies
Login or Register to Ask a Question