Truncating directories 
Author Message
 Truncating directories

Truncating a directory, you may wonder?

It's quite simple: when you keep copies of log files, or backups, from
time to time you want to remove a lot of them, keeping only 5 or 10 of
the most recent files. So I wrote this wonderful module to do it. POD
is appended. The module is easy to use:

  use Dir::Truncate;
  dirtrunc ({keep => 5, verbose => 1}, "/var/logs");

or, from the command line:


Just because I needed it, I wrote it.
Actually, I turned a 5 line shell script into a big module...

My question: what would be a suitable name for the package and the
routine. Maybe the name 'dirtrunc' for the routine is okay. But how to
call the package. In the example I use "Dir::Truncate" but probably a
place under the File:: hierarchy is better.

Please let me know your opinions.

-- Johan

=head1 NAME

Dir::Truncate - Truncate directories to a given number of files.

=head1 SYNOPSIS

  use Dir::Truncate;
  dirtrunc ({keep => 5, verbose => 1}, "/var/logs");


=head1 DESCRIPTION

Dir::Truncate implements one user function: C<dirtrunc>. This function
is exported by default.

The first argument of C<dirtrunc> should either be an integer,
indicating the number of files to keep in each of the directories, or
a reference to a hash with options. In either case, a value for the
number of files to keep is mandatory.

The other arguments are the names of the directories that must be
truncated. Note that this process is not recursive. Also, hidden files
(file name starts with a C<.>) and non-plain files (e.g., directories)
are not taken into account.

All directory arguments and options are checked before anything else
is done. In particular, all arguments should point to existing
directories and the program must have read, write, and execute
(search) access to the directories.

=head1 OPTIONS

=over 4

=item keep

The number of files to keep.

If positive, the newest files will be kept. If negative, the absolute
value will be used and the oldest files will be kept.

=item verbose

Verbosity of messages. Default value is 1. A value of 0 (zero) will
suppress messages.

=item debug

For internal debugging only.

=item test

If true, no files will be removed. For testing.

=back

=head1 EXPORT

Subroutine C<dirtrunc> is exported
by default.

The module does not have other exportable properties.

The variable $Dir::Truncate::VERSION may be used to inspect the version
of the module.

=head1 AUTHOR


=head1 COPYRIGHT AND DISCLAIMER

This program is Copyright 2000 by Squirrel Consultancy. All rights
reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of either: a) the GNU General Public License as
published by the Free Software Foundation; either version 1, or (at
your option) any later version, or b) the "Artistic License" which
comes with Perl.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
GNU General Public License or the Artistic License for more details.

=cut



Sun, 03 Nov 2002 03:00:00 GMT  
 Truncating directories
[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]


Quote:

>It's quite simple: when you keep copies of log files, or backups, from
>time to time you want to remove a lot of them, keeping only 5 or 10 of
>the most recent files.

[...]

Quote:
>My question: what would be a suitable name for the package and the
>routine. Maybe the name 'dirtrunc' for the routine is okay. But how to
>call the package. In the example I use "Dir::Truncate" but probably a
>place under the File:: hierarchy is better.

Perhaps File::Rotate?


Sun, 03 Nov 2002 03:00:00 GMT  
 Truncating directories

Quote:

> My question: what would be a suitable name for the package and the
> routine. Maybe the name 'dirtrunc' for the routine is okay. But how to
> call the package. In the example I use "Dir::Truncate" but probably a
> place under the File:: hierarchy is better.

After several suggestions (lots of them really good, some off-topic),
I intend to use the following naming:

For the package:        Dir::Purge

For the subroutine:     purge

Its default behavior will be to purge by file age.
Other behaviors may be added if necessary.

Thanks all for your feedback, and watch CPAN.

-- Johan



Mon, 04 Nov 2002 03:00:00 GMT  
 Truncating directories

Quote:



> *>>  Dir::PurgeByTime ?
> *>
> *>Nice suggestion, although for me, 'purge' implies that its always the
> *>oldest files that will be removed.
> *>But I admit that my 'remove the newest files' feature is only there
> *>for symmetry purposes, and may not be useful ;-).

> It would appear to fit better in the File:: hierarchy...

> File::SpaceSaver
> File::SpaceMaker
> File::Logtrimmer
> or
> Logfile::SpaceMaker :)

I think it fits better in the Dir:: category. It's not dealing with
one file, but all those in a directory.

Tim.



Mon, 04 Nov 2002 03:00:00 GMT  
 Truncating directories

Quote:

>    Dir::PurgeByTime ?

Nice suggestion, although for me, 'purge' implies that its always the
oldest files that will be removed.
But I admit that my 'remove the newest files' feature is only there
for symmetry purposes, and may not be useful ;-).

-- Johan



Mon, 04 Nov 2002 03:00:00 GMT  
 Truncating directories

Quote:

>=item keep

>The number of files to keep.

>If positive, the newest files will be kept. If negative, the absolute
>value will be used and the oldest files will be kept.

While I can see that file age is the *most* useful criterion by which to
trim a directory, I can also concieve of uses where some other criterion is
useful - for example, file size, or alphabetical filename.

Just a thought.

Anthony Argyriou
Unix _is_ user-friendly. It's just selective about its friends.



Mon, 04 Nov 2002 03:00:00 GMT  
 Truncating directories

Quote:


> > My question: what would be a suitable name for the package and the
> > routine. Maybe the name 'dirtrunc' for the routine is okay. But how to
> > call the package. In the example I use "Dir::Truncate" but probably a
> > place under the File:: hierarchy is better.

> After several suggestions (lots of them really good, some off-topic),
> I intend to use the following naming:

> For the package:   Dir::Purge

Fine by me.

Quote:
> For the subroutine:        purge

Not so fine. A verb with no noun. Why not dir_purge or purge_dir?

Tim.



Mon, 04 Nov 2002 03:00:00 GMT  
 Truncating directories

Quote:

> Not so fine. A verb with no noun. Why not dir_purge or purge_dir?

'purge_dir' is fine with me.

-- Johan



Tue, 05 Nov 2002 03:00:00 GMT  
 Truncating directories


Quote:

>>=item keep

>>The number of files to keep.

>While I can see that file age is the *most* useful criterion by which to
>trim a directory, I can also concieve of uses where some other criterion is
>useful - for example, file size, or alphabetical filename.

>Just a thought.

How about a user hook for sorting by something other than -M?

--
Peter Scott



Tue, 05 Nov 2002 03:00:00 GMT  
 Truncating directories

Quote:


>> Not so fine. A verb with no noun. Why not dir_purge or purge_dir?

>'purge_dir' is fine with me.

so far it looks as if you only intend to purge by file mod time, but
i think it would be interesting to let purge_dir take as an optional
argument a sort subroutine.  it gets rid of whatever sorts towards
the bottom. :)

--
brian d foy
Perl Mongers <URI:http://www.perl.org>
CGI MetaFAQ
  <URI:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>



Tue, 05 Nov 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Join

2. Perl truncate doesn't work but C truncate does?

3. How can I break up directories into different directory components

4. Uppercase / truncated filenames from $0

5. truncate using a globref

6. Traversal of a directory tree with housekeeping per directory

7. How does truncate() know filehandle from filename?

8. FileCache, truncating, unlinking

9. Tricks with truncate()

10. MS SQL Server truncating text fields at 8000 characters during update

11. File truncate with put in NET::FTP

12. Archive::Tar - tar a directory and sub directories

 

 
Powered by phpBB® Forum Software