PDA

View Full Version : How to Delete File when 24 hours old?


trakwebster
02-12-04, 08:36 AM
I wonder if anyone here could tell me how I'd go about writing a bash script which would wipe specified files in a specified directory, but only if those files were so many hours old.

For example (in pseudo-code):

Foreach file in thisdirectory,
if filedate > 24hours
then rm thisfile

Anybody know how to do it?

dechantj
02-13-04, 09:16 AM
I know how to do it in PHP, but unfortunately don't know how to do it in a bash script.

trakwebster
02-13-04, 09:50 AM
In the meantime, I've found rather a simple way to do it. There is a utility (it was already on my server) called tmpwatch, whose function is to delete files older than X hours.

Normally used for the /tmp directory, this utility will work on any directory, so the "bash script" to call it is just:


/usr/sbin/tmpwatch 24 /path/to/dir

(This would wipe all files in the chosen directory which were last accessed more than 24 hours ago.)

Full details available via man tmpwatch.