how to del files older than a calculated date

  • Follow


Hi,

I run a counterstrike server that generates log files of what's going
on with the server.  The logs are starting to take up too much space
on my hard drive.  Log files greater than 30 days old are not needed. 
I'd like to write a daily cron job that automatically deletes logs
greater than 30 days old.

2 questions: how can I calculate the date to be 30 days in the past
and what command can I issue to delete files based on the file system
time stamp?

I'm using BASH on Redhat linux version 9 with a 2.4 kernel.  Any help
is appreciated as always.  Thank you,

Rob
0
Reply rbaxter 10/7/2003 3:33:28 PM

Rob Baxter <rbaxter@cyence.com> wrote:

> 2 questions: how can I calculate the date to be 30 days in the past
> and what command can I issue to delete files based on the file system
> time stamp?

Assuming that you replace "startdir" with the path to the directory
containing your logfiles, the following should delete any ordinary files
in startdir (and any files in subdirectories of startdir) that have not
been modified the last 30 days:

   find startdir -type f -mtime +30 -exec rm -f {} \;

I'd recommend that before you run this the first time, you do this:

   find startdir -type f -mtime +30 -exec ls -l {} \;

and verify that it is picking up *only* the files you really want
to delete.

-- 
Wayne Brown                | "When your tail's in a crack, you improvise
fwbrown@bellsouth.net      |  if you're good enough.  Otherwise you give
                           |  your pelt to the trapper."
"e^(i*pi) = -1"  -- Euler  |           -- John Myers Myers, "Silverlock"
0
Reply Wayne 10/7/2003 4:08:14 PM


>    find startdir -type f -mtime +30 -exec rm -f {} \;
> 
> I'd recommend that before you run this the first time, you do this:
> 
>    find startdir -type f -mtime +30 -exec ls -l {} \;
> 
> and verify that it is picking up *only* the files you really want
> to delete.


That worked perfectly.  Thanks very much.  Now I will research what
find and exec are, and how they work :)
0
Reply rbaxter 10/8/2003 1:42:08 AM

Rob Baxter <rbaxter@cyence.com> wrote:

> That worked perfectly.  Thanks very much.  Now I will research what
> find and exec are, and how they work :)

You're welcome.  Try "man find" for further info.  ("exec" is one of
the built-in options for "find.")

-- 
Wayne Brown                | "When your tail's in a crack, you improvise
fwbrown@bellsouth.net      |  if you're good enough.  Otherwise you give
                           |  your pelt to the trapper."
"e^(i*pi) = -1"  -- Euler  |           -- John Myers Myers, "Silverlock"
0
Reply Wayne 10/8/2003 5:42:49 PM

Rob Baxter wrote:
> Hi,
> 
> I run a counterstrike server that generates log files of what's going
> on with the server.  The logs are starting to take up too much space
> on my hard drive.  Log files greater than 30 days old are not needed. 
> I'd like to write a daily cron job that automatically deletes logs
> greater than 30 days old.
> 
> 2 questions: how can I calculate the date to be 30 days in the past
> and what command can I issue to delete files based on the file system
> time stamp?
> 
> I'm using BASH on Redhat linux version 9 with a 2.4 kernel.  Any help
> is appreciated as always.  Thank you,

Somewhat tangentially,
man logrotate

0
Reply Ian 10/8/2003 9:10:09 PM

4 Replies
537 Views

(page loaded in 0.059 seconds)

Similiar Articles:













7/25/2012 5:06:43 PM


Reply: