Operating System - HP-UX
1834508 Members
2814 Online
110068 Solutions
New Discussion

script - check date and filename and delete files

 
SOLVED
Go to solution
Nitin Nigam
Occasional Advisor

script - check date and filename and delete files

Hi

I have one file with the list of nearly 20 files.
(i.e. filename is testfile and the contents are
tcp
voice
nwint
.
.
etc )

there is one directory in which there are files starting with (tcp, voice....etc)
what I want is that script first go to the testfile and pickup the first file (ex. tcp)from it and than go the directory and check how many files starting with tcp and delete all the files starting with tcp older than 5 days.
I want script to check all the records one by one in testfile and than to the directory.
at the end if there is any file in the directory but does not exist in testfile delete it after 20 days.

thanks
2 REPLIES 2
steven Burgess_2
Honored Contributor
Solution

Re: script - check date and filename and delete files

Hi

Pretty straightforward, with a bit more thought you could probably do it a lot quicker, test first of course

#!/usr/bin/sh

DIR=/directory/to/check

#loop through testfile

for file in $(cat testfile)
do
cd $DIR
find . -name "$file*" -mtime +5 -exec rm -f {} \;
done

# check for files not in testfile

ls -1 | while read checkfile do
grep $checkfile testfile
if [ $? != 0 ]
then
rm $checkfile
done

cd -


HTH

Steve
take your time and think things through
Nitin Nigam
Occasional Advisor

Re: script - check date and filename and delete files

Hi steven,

u hv solved my problem

Thanks