Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi unix script gurus, I am work

Status
Not open for further replies.

mixxalot

Technical User
Mar 5, 2003
6
US
Hi unix script gurus,

I am working on a script and it doesnt quite do the trick right yet. Basically, the script will run from a cron job and function to remove old files of a specific type. Once this one is working, the process will probably be expanded to other file types and/or source directories.

File selection criteria:
1) File location: /ifas/admin/datagc (remove only files from this directory)

2) Because IFAS still has roots from its MPE days, each file has an MPE header record which tells us what program created the file. The files we want to remove were created by the IFAS Ad-Hoc report writer.There are 200 files that need to have this field stripped away:

Example:
The first record for /ifas/admin/datagc/BACTBSG is:
MPE 1028 196 64 50000 0 -1 0 0 -1 0

The string from the above that we need to select on is : " 0 0 -1 0" (there is a space before the first 0). This is the file code for the output from the Ad-Hoc Report Writer.
3) The third criteria is to remove only those files with a last modified date of ?? number of days or more (probably 30 days).


Process
1) The selected files should be moved (mv) from /ifas/admin/datagc to /ifas/admin/trash (new directory). The file should be appended with the directory name that the file came from. Example: /ifas/admin/datagc/BACTBSG is moved to /ifas/admin/trash/BACTBSG.datagc. Be sure to keep the same file permissions and modified date. We haven't yet decided how often to run this script, perhaps weekly.

2) A separate script/cron job (not yet written) will then do the actual delete from ./trash for files last modified ?? number of days or more (probably 90 days). This allows the users a last chance to keep the file before it is deleted.

Here is what I have thus far:

#!/usr/bin/ksh
#
SOURCE_DIR=/ifas/admin/datagc
TARGETA_DIR=/ifas/admin/datagc
#
find $SOURCE_DIR -type f -mtime +30|while read FILE
# find files>30 days old
do
FIRST_RECORD=`head -1 $FILE` # read first record of file
echo $FIRST_RECORD
FILE_FOUND=`grep " 0 0 -1 0" $FILE|wc -l` # set flag to 1 if record matches pattern
if [ $FILE_FOUND -eq 1 ]
then
cd $SOURCE_DIR
FILE_NAME=`echo $FILE|awk -F"/"'{print $NF}'`

# get rid of directory path from file to show just the file
#
find . -name "$FILE_NAME"|-mivd>archive.cpio

# create cpio archive of file includeparent dir
mv archive.cpio $TARGET_DIR

# move archive to target directory
cd $TARGET_DIR
cpio -mivd<archive.cpio
# unarchive cpio archive and create other dirs if needed
fi
done
#
# end of script
However I am still stuck. It doesnt move the cleaned up files with the field stripped to the new directory. I am trying to cut this MPE field out and move the modified file to the directory. Thanks for your help

Ben Prusinski
master85@hotmail.com
 
For the file name/rename parts try :
if [ $FILE_FOUND -eq 1 ]
then
cd $SOURCE_DIR
FILE_NAME=$(basename $FILE).$(basename `pwd`)
mv $FILE $FILENAME
etc
etc
Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top