[K12OSN] OT: Scripting Question

Eric Harrison eharrison at mail.mesd.k12.or.us
Thu Nov 30 18:26:10 UTC 2006


Jim Kronebusch wrote:
> I hate to post another scripting question out here again, but you 
> guys are so darn good it would be a shame not to :-)
> 
> I have a folder full of files, I need a script that deletes any files
>  in that folder that are more than 30 days old.  Sort of an automatic
>  pruning.  I will start google'ing as well but thought I would throw 
> this out here quick.  I would think it would be something like take 
> current date - 30 and then remove any files with a date less than 
> that result.  But I have no idea of what syntax to use with dates, 
> usually not as simple as basic math because if variable length 
> months.
> 


How about using the tmpwatch utility?

To delete files & folders in /FOLDER that have not been accessed in the
last 30 days:

	tmpwatch 720 /FOLDER


To delete files & folders in /FOLDER that have not been modified in the
last 30 days:

	tmpwatch -mtime 720 /FOLDER


-Eric


man tmpwatch


DESCRIPTION

tmpwatch  recursively removes files which haven't been accessed for a
given number of hours. Normally, it's used to clean up
directories which are used for temporary holding space such as /tmp.

When changing directories, tmpwatch is very sensitive to possible race
conditions and will exit with  an  error  if  one  is detected.  It does
not follow symbolic links in the directories it's cleaning (even if a
symbolic link is given as its argument), will not switch filesystems,
skips lost+found directories owned by the root user, and only removes
empty  directories regular files, and symbolic links.

By  default,  tmpwatch  dates files by their atime (access time), not
their mtime (modification time). If files aren't being removed when ls
-l implies they should be, use ls -u to examine their atime to see if
that explains the problem.

If the --atime, --ctime or --mtime options are used in combination, the
decision about deleting a file will be based on  the maximum of these
times.  The --dirmtime option implies ignoring atime of directories,
even if the --atime option is used.

The hours parameter defines the threshold for removing files. If the
file has not been accessed for hours hours, the file is removed.
Following this, one or more directories may be given for tmpwatch to
clean up.




More information about the K12OSN mailing list