The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
server monitor script... zedex Shell Programming and Scripting 1 06-01-2008 04:10 PM
need help doing a script to monitor if files are go through jonathan184 Shell Programming and Scripting 0 05-15-2007 11:47 AM
load monitor script locabuilt UNIX for Advanced & Expert Users 7 01-19-2007 02:37 PM
Script to Monitor databases help with arrays nelmest Shell Programming and Scripting 1 09-05-2005 10:50 PM
Monitor which users enter my home directory mnpradeep High Level Programming 1 03-21-2002 05:08 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-24-2006
cmf00186 cmf00186 is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 2
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 (permalink)  
Old 10-25-2006
mahendramahendr mahendramahendr is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2005
Location: London
Posts: 222
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 (permalink)  
Old 10-25-2006
cmf00186 cmf00186 is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 2
mahendramahendr,

I tried your code and it works perfectly!. Thank you very much for your help.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0