Operating System - HP-UX
1835101 Members
1991 Online
110073 Solutions
New Discussion

Re: need help with writing a script to do the following cleanup tasks

 
Ben Prusinski
Occasional Advisor

need help with writing a script to do the following cleanup tasks

Hello my fellow sysadmins and gurus,

I have not written scripts in a while and would greatly appreciate your help on creating a unix shellscript to do the following on our hp9000 hpux 11.00 server:

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 so much for your help!

Ben Prusinski
System Administrator
benp@csus.edu
"that which does not kill you makes you stronger"- Nieschze
3 REPLIES 3
Tim Lane
New Member

Re: need help with writing a script to do the following cleanup tasks

Ben

1. Finding files in a directory with a string can be done with the grep command:

grep -l " 0 -1 0 0 -1 0" *

Although this will check every file in the dir and every line in the file but it looks pretty unique.

2. Removing old files is pretty straight-forward using the find command with the exec option.

e.g.
/usr/bin/find / \( -name '' \) -mtime +2 -exec rm -r {} \;

this removes files older than 2 days.



3. To add the directory name to the files when they are moved, the following code segment will do this:

e.g.

DIR_NAME=`pwd | sed 's/\//./g'`
mv test test.$DIR_NAME

the sed command removes all the '/' and replaces them with '.'.

Use these techniques and string them together into a script and you should be OK.

Regards,

Tim .








curt larson_1
Honored Contributor

Re: need help with writing a script to do the following cleanup tasks

here is a good start:

#!/usr/bin/ksh

fromDir=
toDir=
append=
out=crap

cd $toDir
find . -mtime +30 |
sed '/.\/.*\/.*/d' | #remove files in subdirectories
while read file
do
out=$(cat $file | awk '/NR==1/ { if ( $0 ~ "" ) print "match";"
/NR>1/ {exit;}')
if [[ $out = "match" ]] ;then
print $file | cpio -pm $toDir
rm $file
mv $toDir/$file $toDir/${file}.$append
out=crap
fi
done

you might want to add some error checking. such as your toDir exists, the cpio was sucessfull before removing the file, etc.
Christian Gebhardt
Honored Contributor

Re: need help with writing a script to do the following cleanup tasks

Hi

find /ifas/admin/datagc -mtime 90 -type f -exec grep -il " 0 0 -1 0" {} \; | xargs rm

explanation:

- find only in the directory /ifas/admin/datagc
- only files with modification time > 90
- only files (no links or dir's)
- containing search-string " 0 0 -1 0"
- removing this files

Chris