Operating System - HP-UX
1833059 Members
2631 Online
110049 Solutions
New Discussion

ksh script for removing old files

 
SOLVED
Go to solution
David Bell_1
Honored Contributor

ksh script for removing old files

I'm trying to create a simple script that would remove files in an archive. Since the naming is close to the same, I'd like to do it by date. Ideally, I'd like to keep 1-2 months of data. That being said, I could do it by grep "Jan" or something like that or I could do it with something utilizing $(( (90-$(date +%j)) )) or something similar. Currently the directory contains files beginning January 1 through today. I'd like to keep May and June. Going forward, when run, it should maintain 2 months of data in the directory. Any help would be appreciated. As you can tell, scripting is not my strong suit...

Thanks,

Dave
7 REPLIES 7
John Dvorchak
Honored Contributor
Solution

Re: ksh script for removing old files

Very simply you can:

find /your/dir -type f -mtime +60 -exec rm {} \;

Be very carefull with find and "-exec rm" make sure that you use an EXPLICIT file location. NEVER cd to directory and start removing files in a script. That will bite you someday when the directory is moved/renamed etc.

Good luck
If it has wheels or a skirt, you can't afford it.
Dave La Mar
Honored Contributor

Re: ksh script for removing old files

Dave -
find /tmp/ -type f -mtime -1 > $FILE_LIST_TMP 2>$RESULTS_FILE
==> used to build a list of files created in the last 24 hours. Then using this list simply do a for loop on a cat of the file to perform the rm on.

Man find, you will find other time options. In this case -1 is the interval of 24 hour periods.

Best of luck.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
A. Clay Stephenson
Acclaimed Contributor

Re: ksh script for removing old files

Well, it could be something as simple as

find dirname -type f -mtime +60 -exec rm {} \;

substitute something innocuous like "echo" for the rm until you feel safe. This will delete all regular files older than 60 days undir dirname (and it's subdirectories).

The date +%j stuff is not very robust because of end of year issues.

If you must you something that converts Date encoded filenames into meaningful numeric values then I would use caljd.sh. It will easily parse the Monthnames (in any language). Search the forums for caljd.sh and you should get a number of hits. Get version 2.1; it's the latest
.

If you will give a few exact examples of the filenames, I can probably write an example caljd.sh call.
If it ain't broke, I can fix that.
Geoff Wild
Honored Contributor

Re: ksh script for removing old files

I have something like:

#!/bin/sh
# Script to remove old print jobs
# Geoff Wild
# April 4 2002
#
find /var/spool/lp/receive -name '*III' -mtime +7 -exec rm {} \; >/tmp/lp.receive.log
find /var/spool/lp/receive -name '*AAA' -mtime +7 -exec rm {} \; >>/tmp/lp.receive.log
find /var/spool/lp/receive -name '*EEE' -mtime +7 -exec rm {} \; >>/tmp/lp.receive.log
find /var/adm/lp/XEB -name '*ftplog' -mtime +7 -exec rm {} \; >>/tmp/lp.receive.log


RGds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
David Bell_1
Honored Contributor

Re: ksh script for removing old files

Guys,

All good and viable examples. I thank you for the assistance. For the life of me I can't see how but I missed the -exec!!!!

Clay,

Thanks for the info on the caljd.sh. I'll look into it and post if I can't figure out how to utilize it. The information provided is quite sufficient for now.

Thanks again,

Dave
Jordan Bean
Honored Contributor

Re: ksh script for removing old files


More efficient than

find /path -type f -exec rm {} \;

is

find /path -type f -exec rm \+

which works just like

find /path -type f | xargs rm


As far as I know, this is only true with HP-UX find. For portability, use the find|xargs pipeline when working with long lists.

David Bell_1
Honored Contributor

Re: ksh script for removing old files

Jordan,

Thanks for the additional information.

Dave