1848533 Members
4410 Online
104033 Solutions
New Discussion

Re: Remove audit files

 
SOLVED
Go to solution
Derek Card
Advisor

Remove audit files

Hello,

I have a directory /user1/audit which has hundreds of files in it with names like "01-Aug-2002", "12-Dec-2001", and "13-Jun-2000". We only need to keep the files that are less than 31 days old. Is there a command to remove these files?

TIA, Derek
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Remove audit files

Sure.

find /user1/audit -type f -mtime +30 -exec rm {} \;

I would first try this command with a safe command like -exec echo {} \; before doing the real thing.
If it ain't broke, I can fix that.
Derek Card
Advisor

Re: Remove audit files

Hello again,

I forget to add before anybody does a find -mtime that I can't use find because the file "12-Dec-2001" might not have been created on December 12, 2001. I know it's dumb but I didn't write the software. I actually need to figure out how old a file is based on the name of the file.

TIA, Derek
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Remove audit files

Okay Derek, you want to make this complicated. Well, this is a job for my date sledgehammer, caljd.sh.

This should be very close:

#!/usr/bin/sh

DIRNAME=/user1/audit
MAX_DAYS=30

cd ${DIRNAME}
TODAY=$(caljd.sh)
for F in $(ls)
do
if [[ -f ${F} ]]
then
echo "File: ${F}\t\c"
AGE=$((${TODAY} - $(caljd.sh -e -i -S "-" -c ${F})))
echo "Age: ${AGE} days\c"
if [[ ${AGE} -gt ${MAX_DAYS} ]]
then
echo "\tRemove\c"
# rm ${F}
fi
echo
fi
done

You can invoke caljd.sh -u for full usage. Make sure that you use Version 2.1; earlier versions could not handle dates with names of months.

Here is caljd.sh.


If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Remove audit files

If you prefer, you can use the Perl version, caljd.pl. The arguments are exactly the same simply substitute caljd.pl for caljd.sh.

Note that I commented out the actual rm command. After you are satified with the results, you can uncomment the command.


Attached is caljd.pl.

Regards, Clay

If it ain't broke, I can fix that.
Derek Card
Advisor

Re: Remove audit files

Thanks very much, Clay! I removed the comments from the rm command and it worked perfectly! I'm going to now make a cron entry to run this command.

Thanks again, Derek
A. Clay Stephenson
Acclaimed Contributor

Re: Remove audit files

Hi Derek:

Before you turn this into a cronjob, you need to make sure that caljd.sh is in the PATH and exported. I would do this explicitly in the script. Cron has an intentionally sparse environment. I would also remove all the echo commands or add a command line option for verbose if you want to keep them in.

Regards, Clay
If it ain't broke, I can fix that.