Operating System - HP-UX
1831262 Members
2749 Online
110022 Solutions
New Discussion

Re: Removing files based on size, date etc.

 
SOLVED
Go to solution
Sanjay Verma
Super Advisor

Removing files based on size, date etc.

Hi Friends,
I would like to create a script which takes parameters like - directory, size, days old etc. so that I can clear the files based on the above criteria. I would like to have the flexibility to modify the configuration files easily. Will appreciate for your comments on this.
Co-operation - The biggest chain reaction
7 REPLIES 7
Con O'Kelly
Honored Contributor
Solution

Re: Removing files based on size, date etc.

There's a multitude of different ways to do this. One option is to write a script using find command:
e.g.
# find / -name -size -atime -exec rm {} \;

Define the variables DIR, SIZE, TIME, NAME etc in a config file and source this config file from your script.

A note of caution: You need to test this throughly first to ensure you don't delete any important files. Use 'll' in place of 'rm' in the find command to see what files are found.
Also it's safer if possible to use wild card names to rm files.

Cheers
Con
Michael Steele_2
Honored Contributor

Re: Removing files based on size, date etc.

Most people just use the 'find' command for all of these.

Date: Refer to 'atime' (* last accessed *)and 'mtime' (* last modified *) arguements in 'find' command.

-atime +7 (* greatet than 7 days *)
-atime 7 (* equal to 7 days *)
-atime -7 (* less than 7 days *)

find /dir -atime +7

Clear the files: Is that remove or zero out? That would be '-exec rm {} \;' to remove.

find /dir -atime +7 -exec rm {} \; (* remove all files greater than 7 days in /dir. *)
Support Fatherhood - Stop Family Law
Sanjay Verma
Super Advisor

Re: Removing files based on size, date etc.

Thanks Friends. To achieve the task, I would like to have the configuration setup in the format below:


#Files/Directories --- Size --- Days
/home/user1 64
/home/user3 10

To explain this, I need to configure the size and days where applicable and then have the command pick-up the values as env. variables.
(Note - days is not specified for user1 whereas size is not specified for user3)

Hope this's not a complex task.
Co-operation - The biggest chain reaction
Huc_1
Honored Contributor

Re: Removing files based on size, date etc.

Could you not simply use crontab
like ex:

00 22 * * * /bin/find /home/user1 -size 1000 -atime -3 -exec rm {} \; > user1_rm.log
05 22 * * * /bin/find /home/user3 -size 300 -atime -7 -exec rm {} \; > user3_rm.log

I would first test this with -exec ls -l {} \; in crontab.

But then perhaps I did not understand your question fully ?

did it do the job for you ? then ... well glad I could be of help.

Jean-Pierre
Smile I will feel the difference
James R. Ferguson
Acclaimed Contributor

Re: Removing files based on size, date etc.

Hi Sanjay:

Create a function within a script that expects two arguments -- the name of the directory and the number of days after which unmodified files are purged. The script would look something like this:

#!/usr/bin/sh
function prune_directory
{
find $1 -mtime +$2 -type f -xdev|xargs -L 500 -i rm -f {}
}
#
prune_directory /home/user1 64
prune_directory /home/user3 10
exit 0

Regards!

...JRF...
Sanjay Verma
Super Advisor

Re: Removing files based on size, date etc.

Friends, I'm working with all the options and will update with my outcome.
Co-operation - The biggest chain reaction
Srinivas Thokala_1
Frequent Advisor

Re: Removing files based on size, date etc.

# find /var/tmp ???mtime 5 ???exec ll {} \; to display files older than 5 days.
# find /var/tmp ???mtime 10 ???exec rm {} \; to remove files older than 10 days.

Removes under /tmp not been accessed more than 14days.
# find /tmp ???type f ???atime +14 ???exec rm {} \;

To remove a directory not been updated with files more then 14 days is :
# find /tmp ???type d ???atime +14 ???print ???exec rmdir {} \;

To remove a.out and *.o files
# find / -name ???a.out??? ???print > aout.list
# find / -name ???*.o??? ???print > o.list

To remove core files :
# find / -name ???core??? ???type f ???atime +14 ???exec rm {} \;

-Srini
Srinivas Thokala