in reply to Deleting files over 2 weeks old?

A minor correction: some of the posters above used -C or "ctime" and mentioned them in the same breath as "create time". There's no create time in Unix. The "ctime" value is the "last inode change" value: the last time anything "changed" about the file, such as contents, permissions, ownership, or number of links (including renaming).

"Files over two weeks old" is often an ambiguous specification. For deleting items from a cache, I suggest the "atime" value (most recent access), as it is often indicative of an orphan when there are no more accesses in a long time.

As for using Perl to zap files with big atimes, I'd go for a command-line-written chunk of code:

$ find2perl /some/dir -atime +14 -eval unlink >MyProggy $ chmod +x MyProggy $ ./MyProggy # as you wish

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
(jcwren) RE: Re: Deleting files over 2 weeks old?
by jcwren (Prior) on Aug 03, 2000 at 02:47 UTC
    An excellent point on the ctime, merlyn. However, why redirect and execute, as opposed to piping it into 'xargs', and using 'rm'?

    --Chris

    e-mail jcwren
      OK, let's take the classic root crontab code:
      find /tmp /usr/tmp -atime +7 -print | xargs /bin/rm -f
      And then I come along (as an ordinary user) and do the following:
      $ mkdir -p "/tmp/foo /etc" # yes, that's a newline after foo before the /etc $ touch "/tmp/foo /etc/passwd" # yes, that's a newline after foo again
      And then sit back 7 days. Boom. You have no /etc/passwd.

      The problem is that you are using newline as a delimiter, and yet it is a legal filename character. You need find .. -print0 and xargs with a -0, but that's not portable. Even though Perl isn't strictly everywhere, it's everywhere the perlmonks are, so my solution succeeds in a safe way.

      -- Randal L. Schwartz, Perl hacker

        Ouch! nice one merlyn Can I vote myself down for leaving crap find commands on nearly every server I've accessed?
        I would have used:
        find /somewhere/or/something -atime +7 -exec rm -v {} \;
        Then there arn't any chars that can freak it out. If not a -exec I simply wouldn't use find for it. update: ahh... an excelent point merlyn. Perl rules!

        Got Perl?