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!

need help writing cleanup script in hp-ux

Status
Not open for further replies.

mixxalot

Technical User
Mar 5, 2003
6
US
Hello fellow sysadmins and programmers. I have not done scripting in a long time and would greatly appreciate your assistance in creating a script to do the following cleanup task

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.
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

Thanks a million!
Ben
master85@hotmail.com
 
#!/usr/bin/ksh
# -type f denotes files not directories or links
# -mtime +30 denotes files that have not been modified in more than 30 days

SOURCE_DIR=/ifas/admin/datagc
TARGETA_DIR=:/ifas/admin/datagc

find $SOURCE_DIR -type f -mtime +30|while read FILE # look for files in the source dir more than 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 first record matches pattern
if [ $FILE_FOUND -eq 1 ]
then
cd $SOURCE_DIR
FILE_NAME=`echo $FILE|awk -F"/" '{print $NF}'` #strip dir path off of file to show just file name
find . -name "$FILE_NAME"|cpio -mivd > archive.cpio # create a cpio archive of file including parent directory
mv archive.cpio $TARGET_DIR # move archive to target directory
cd $TARGET_DIR
cpio -mivd < archive.cpio # unarchive cpio archive, relative directories will be created as necessary.
fi
done Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top