The UNIX and Linux Forums  

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 01:10 PM
need help doing a script to monitor if files are go through jonathan184 Shell Programming and Scripting 0 05-15-2007 08:47 AM
load monitor script locabuilt UNIX for Advanced & Expert Users 7 01-19-2007 10:37 AM
Script to Monitor databases help with arrays nelmest Shell Programming and Scripting 1 09-05-2005 07:50 PM
Monitor which users enter my home directory mnpradeep High Level Programming 1 03-21-2002 01:08 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-24-2006
Registered User
 

Join Date: Oct 2006
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-25-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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...
Reply With Quote
  #3 (permalink)  
Old 10-25-2006
Registered User
 

Join Date: Oct 2006
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
mahendramahendr,

I tried your code and it works perfectly!. Thank you very much for your help.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix daemon process export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix simulator unix.com vi select all vi substitute vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 05:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101