Re: OT: Shell scripting wizardry needed


Subject: Re: OT: Shell scripting wizardry needed
From: nathan r. hruby (nhruby@arches.uga.edu)
Date: Thu Dec 20 2001 - 12:00:51 MST


On Thu, 20 Dec 2001, Gawain wrote:
>
> In the interest of helping others who may have the same problem,
> here's the command that finally did the trick:
>
> # REMOVE FILES AND DIRECTORIES OLDER THAN A CERTAIN DATE (20 days in this case)
> find /foo/bar -mtime +20 -maxdepth 1 -exec rm -rf '{}' ';'
>
> Obviously, any command that deletes files has the potential to cause
> tremendous damage to your system. Be careful! I did a lot of testing
> before adding the line to my shell script.
>

Sorry to chime in late, but tmpwatch will also do what you need as well,
in fact it's written spcficly to do what you need.

From the man page:
      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 hold-
       ing 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,
        and only removes empty directories and regular files.

       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.

To whit, if someone had a symbolic link to / in /foo/bar, you'd probably
end up deleting your entire filesystem with the find command you list
above. I'd suggest adding the -mount (stay on current filesystem) option
and adding a -type flag to ignore symbolic links, or to be further
pedantic, specfiy "-type fd" to find files and directories only.

A super secure peson would write two find commands, one for files the
simply rm's the old crufty files, and one that runs right after that
rmdir's old crufty directories. Thus getting rid on any kind of
nastytitude that could be casued by running "rm -rf" as root.

-n (.sig eaten by grue)



This archive was generated by hypermail 2a24 : Thu Dec 20 2001 - 12:14:43 MST