![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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
|
|
||||
|
mahendramahendr,
I tried your code and it works perfectly!. Thank you very much for your help. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|