diff options
author | Peter Palfrader <peter@palfrader.org> | 2006-10-18 12:43:06 +0000 |
---|---|---|
committer | weasel <weasel@bc3d92e2-beff-0310-a7cd-cc87d7ac0ede> | 2006-10-18 12:43:06 +0000 |
commit | d757d8ace65e0e64ab4fd4fa2d2173956ff7086e (patch) | |
tree | 4fe6936092fdc7fe2dbf91d434dcc05976797f33 /keep-num | |
parent | afbb53a9f72db72ed65352d75b2fb44eeae39b93 (diff) |
Add Flo's keep-num
git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@201 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede
Diffstat (limited to 'keep-num')
-rwxr-xr-x | keep-num | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/keep-num b/keep-num new file mode 100755 index 0000000..bf89f2f --- /dev/null +++ b/keep-num @@ -0,0 +1,48 @@ +#! /usr/bin/ruby + +# Given a directory, print the filenames of all but the last (alphabetically) n files +# printed filenames are separated by a \0 character, suitable for use with xargs -0 +# +# Copyright (c) 2005 Florian Reitmeir +# +# MIT license +# + +require 'optparse' + +$NULL = nil +$dir = nil +$num_to_keep = 0 +$prefix = nil + +def show_help(parser, code=0, io=STDOUT) + io.puts parser + exit(code) +end +ARGV.options do |opts| + opts.on_tail("-h", "--help", "Display this help screen") { show_help(opts) } + opts.on("-p", "--path=PATH", String, "path to cleanup") { |$dir| } + opts.on("-n", "--number=NUMBER", Integer, "number of entries to keep") { |$num_to_keep| } + opts.on("-x", "--prefix=PREFIX", String, "prefix of files") { |$prefix| } + opts.on("-0", "--null", String, "terminate strings with zero") { $NULL = 1 } + opts.parse! +end + +show_help(ARGV.options,1,STDERR) unless $num_to_keep +show_help(ARGV.options,1,STDERR) if $num_to_keep < 1 +show_help(ARGV.options,1,STDERR) unless $dir +show_help(ARGV.options,1,STDERR) unless $prefix + +entries = Dir.glob($dir + '/' + $prefix+"*").sort.reverse + +if entries.length < $num_to_keep + exit +end + +while entries.length > $num_to_keep + unless $NULL then + puts entries.pop + else + $> << entries.pop << "\0" + end +end |