Operating System - HP-UX
1826056 Members
4162 Online
109690 Solutions
New Discussion

quick script to houseclean old files

 
SOLVED
Go to solution
James Barry Donovan
Occasional Contributor

quick script to houseclean old files

Good Morning,

Their was a question that arose. We are beginning on a new Unix venture and our application developer needs to write a script to control old files. He needs to check the age of a group of files, if the age exceeds a certain # of days then perform a remove. How could we accomplish this.

Thanks!

-barry donovan
pfpc global fund services
4 REPLIES 4
Joseph C. Denman
Honored Contributor

Re: quick script to houseclean old files

James,

Delete dat files that have not been modified for 7 days.


find /the/dir -type f -name *.dat -mtime +7 -exec rm {} \;

man find

...jcd...


If I had only read the instructions first??
Sebastian Galeski_1
Trusted Contributor

Re: quick script to houseclean old files

Hi
I am not sure if it resolve your problem but try to use next command (lets say we are interested in file located in folder /folder)

#find /folder -atime +30 -exec rm {} \;

it finds all files older than 30 days in /folder and erase it.

hope it help
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: quick script to houseclean old files

Hi:

Try something like this.

DIR=/home/mydir

find ${DIR} -type f -mtime +10 -name '*.dat' -exec rm {} \;

That will find all regular files (-type f) older than 10 days (-mtime +10) that end with '.dat' (-name '*.dat') and then call rm on that file.

Man find for details and make sure that you do something like -exec echo {} \; as a test before you substitute the rm command in the real thing.

Regards, Clay
If it ain't broke, I can fix that.
Helen French
Honored Contributor

Re: quick script to houseclean old files

Hi,

Use the 'find' command with '-mtime' option.

For eg:

# find /test -xdev -type f -mtime +10 -exec rm {} \;

This will delete the files which are not modified within last 10 days from /test.

HTH,
Shiju
Life is a promise, fulfill it!