Hep with script to monitor directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Hep with script to monitor directory
# 1  
Old 10-24-2006
Hep with script to monitor directory

Hello,

I am a newbie who is attempting to write a script to monitor a directory for a set of 3 files that I am expecting to get ftp'd. Occasionally, we suspend operations for maintenance etc. but we still get the files so there can be more than 1 set. If there is more than 1 set, I would like to move all but the latest set to an archive directory.

For example say the files are named:
acme1_090106.txt
acme1_091006.txt
acme1_092206.txt
acme2_090106.txt
acme2_091006.txt
acme2_092206.txt
acme3_090106.txt
acme3_091006.txt
acme3_092206.txt

I would like to move the older files to an archive directory
acme1_090106.txt
acme1_091006.txt
acme2_090106.txt
acme2_091006.txt
acme3_090106.txt
acme3_091006.txt

I am running AIX version 5.31.

This is what I have so far. Any suggestions would be deeply appreciated.

#!/bin/ksh
#
files=0
LoopCnt=0
while (( $files < 4 )); do
echo `date`
for name in `ls acme*`; do
if [ -f $name ]
then let files=files+1
fi
done
if (( $files < 3 )); then
if ((LoopCnt < 6)); then
echo "do not have 3 files yet, sleeping 10 minutes"
echo ""
sleep 600
files=0
let LoopCnt=LoopCnt+1
else
exit
fi
fi
done
This is where I'm stuck

if (( $files > 3)); then
echo "Have more than 3 files, move all but the latest to archive”
exit
fi
# 2  
Old 10-25-2006
There are many ways in doing it... one is based on the time the file created in the system and the other way is based on date in the file name...

There will be a problem with the first method if the latest file ftp'd before the old files... old files will have latest file creation date than latest files and will move latest files to the archive folder...

Hence I adopted the second method which archives the file based on the date in the file name... hope this helps.

Code:
#!/usr/bin/ksh

for name in $(ls acme* | sed 's/_[0-9]*.txt//g' | uniq)
do
   cnt=0
   for fname in $(ls $name* | sed 's/_\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/_\3\1\2/g' | sort -r )
   do
      echo $fname
      nm=$(echo $fname | sed 's/_\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/_\2\3\1/g' )
      cnt=$(($cnt+1));
      if [ $cnt -ne 1 ]
      then
          mv $nm  ./bkup
          echo "$nm moved to backup"
      fi
   done
done

I have tested and it worked fine...
# 3  
Old 10-25-2006
mahendramahendr,

I tried your code and it works perfectly!. Thank you very much for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script monitor directory and subdirectories for new pdfs

I need bash script that monitor folders for new pdf files and create xml file for rss feed with newest files on the list. I have some script, but it reports errors. #!/bin/bash SYSDIR="/var/www/html/Intranet" HTTPLINK="http://TYPE.IP.ADDRESS.HERE/pdfs" FEEDTITLE="Najnoviji dokumenti na... (20 Replies)
Discussion started by: markus1981
20 Replies

2. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

3. Programming

Need hep with my semantic error

while(EOF != (c = fgetc(filePtr))) { if ((c == ' ') || (c == '\n') || (c == '\0')) { if (c == '\0') { continue; } printf("%s",cc); printf("%c\n"); memset(cc, 0, sizeof cc); } else { cc=c; i++; } (5 Replies)
Discussion started by: fwrlfo
5 Replies

4. Shell Programming and Scripting

Script to monitor directory size of specific users

Hi, i am new to shell scripts, i need to write a script that can monitor size of directory of specific users. Please help. Thanks, Nitin (2 Replies)
Discussion started by: nicksrulz
2 Replies

5. Shell Programming and Scripting

Directory Monitor in KSh

Hi, I need to write a directory monitor, i.e. a korn shell script which would Report changes to the directory contents, like: added file1, deleted file2, updated file3 , created subdir (optional)... There is no specific file pattern. So far I have written a little script... (1 Reply)
Discussion started by: olegkon
1 Replies

6. UNIX for Dummies Questions & Answers

Monitor directory and email

Hello all, Can anyone please guide / help me in the following task.... I have a directory where some external users will upload pdf files. The filename of these pdf will be of a particular format (<id>-<first name>_<last name>_<some number>.pdf) I want to make a script such that it takes... (6 Replies)
Discussion started by: dhawalkv
6 Replies

7. AIX

monitor directory events

I'm am looking for a cheap way to trigger a script when a new file is written in a specific directory. AIX 5.3. It is a production system, so no kernel patching (i.e. inotify). Filemon and audtiing are too expensive. Thanks in advance. (2 Replies)
Discussion started by: pbillast
2 Replies

8. Shell Programming and Scripting

Monitor capacity of directory

Good morning. I have been attempting to find a way to monitor the capacity of a directory so that when it reaches 80% or higher I can send an event. I was able to find a script that does this for the whole drive by I can not seem to figure out how to do this for just a single directory. ... (1 Reply)
Discussion started by: LRoberts
1 Replies

9. Shell Programming and Scripting

script to monitor files in a directory and sending the alert

Hi All, We are having important config files in an directory which was accessable by all /auto/config/Testbed/>ls config1.intial config2.intial config3.inital often we find that some of the lines are missing in config files, we doubt if some one is removing. I would like to write... (0 Replies)
Discussion started by: shellscripter
0 Replies

10. Shell Programming and Scripting

script to monitor directory

What is the best way for a script to run to monitor a directory for the presence of files and then perform a function afterwords? I was hoping to have it continually run and sleep until it detects that files are present in the directory, then break out of the loop and go on to the next step. ... (17 Replies)
Discussion started by: nulinux
17 Replies
Login or Register to Ask a Question