From 5c8816c0b98045e9e3dbff1920f60f6ba3831fd3 Mon Sep 17 00:00:00 2001 From: hjp Date: Fri, 9 Jul 1999 21:05:26 +0000 Subject: [PATCH] Added cleandir --- cleandir/.vimrc | 6 +++++ cleandir/cleandir | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 cleandir/.vimrc create mode 100755 cleandir/cleandir diff --git a/cleandir/.vimrc b/cleandir/.vimrc new file mode 100644 index 0000000..82155f2 --- /dev/null +++ b/cleandir/.vimrc @@ -0,0 +1,6 @@ +version 5.0 +map!  >I<yypa/O +set autoindent +set exrc +set number +set shiftwidth=4 diff --git a/cleandir/cleandir b/cleandir/cleandir new file mode 100755 index 0000000..545ba47 --- /dev/null +++ b/cleandir/cleandir @@ -0,0 +1,64 @@ +#!/usr/bin/perl -w +use strict; +use File::stat; + +my $verbose = 0; + +sub usage { + print STDERR "Usage: $0 [-d days] dir ...\n"; + exit(1); +} + +sub cleandir { + my ($dir, $since) = (@_); + my $notremoved = 0; + + if ($verbose) { + print STDERR "$0: cleandir $dir $since {\n"; + } + if (!opendir(DIR, $dir)) { + printf STDERR "$0: cannot opendir $dir: $!"; + return; + } + for my $i (readdir(DIR)) { + if ($i eq "." || $i eq "..") {next} + if ($verbose) { + print STDERR "$0: checking $dir/$i\n"; + } + my $st = lstat("$dir/$i"); + if ($verbose) { + print STDERR "$0: mtime=", $st->mtime, " atime=", $st->atime, "\n"; + } + if (-d _) { + + if (cleandir("$dir/$i", $since) == 0 && $st->mtime < $since) { + if (rmdir("$dir/$i")) {next} + } + } elsif ($st->mtime < $since && $st->atime < $since) { + if (unlink("$dir/$i")) {next} + } + $notremoved++; + } + if ($verbose) { + print STDERR "$0: cleandir: $notremoved }\n"; + } + return $notremoved; +} + +sub main { + my $since = time() - 14 * 86400;; + my $i; + while ($i = shift(@ARGV)) { + if ($i eq "-d") { + my $days = shift(@ARGV); + $since = time() - $days * 86400; + } elsif ($i eq "-v") { + $verbose++; + } else { + cleandir($i, $since); + } + } + exit(0); +} + +main();