About Archive Tags RSS Feed

 

moreutils makes a lot of sense to me

15 April 2012 21:50

I've installed moreutils on several hosts now, and each time I find a new use for one of those tools I'm very happy.

Unfortunately I suspect that too many of us continue to hoard our little shell script archives, continuing to re-invent wheels with slightly different colours, shapes, and sizes.

I suspect I've just re-invented the wheel again, writing nrecent, because there seems to be no "simple" way of doing the job of keeping the most N recent files in a directory.

I guess the standard approach would be to use "ls", or "find", to find all files in a directory, reverse-sorted by mtime, then use "tail +n2 to skip the ones you want to keep, removing the rest.

Anyway nrecent - keep the most recent N files in a directory:

nrecent --keep 20 /tmp

Written as naively/hurridly as possible as it solved, and continues to solve, a real need.

ObQuote: "You're like the drummer from REO Speedwagon. Nobody knows who you are. " - Employee of the month

| 3 comments

 

Comments on this entry

icon mirabilos at 15:45 on 15 April 2012
http://packages.debian.org/mksh

If your shell snippets are of good quality, https://evolvis.org/projects/shellsnippets/ is a place to put them. The focus of that collection is more as a teaching ground for shell coders of any couleur, those who learn by reading other peoples’ code, than ready-to-run scripts and programmes, but such are included there. (Maybe some day we can include a top-level index.htm which lists the snippets and comments on them, ordered, possibly hierarchically.)

icon Steven Chamberlain at 18:59 on 16 April 2012
http://pyro.eu.org/

If this is for backups, why keep the 10 most recent? I came up with an algorithm to keep the current, previous, but after that the 2^nth most recent and so on. I really ought to write it up someday.

It gives a 'spread' with mostly recent backups but keeps a few older versions archived. It works great for full backups or snapshots, but it doesn't really suit incrementals.

icon Steve Kemp at 19:01 on 16 April 2012
http://steve.org.uk/

Not backups in this case, more logfiles archives of images, etc.

(e.g. I shoot a model, and I upload to http://stolen-souls.com/temporary/16-Steve.zip - I later go back and delete old uploads. But just pruning to the most recent three, four, or five would be useful.)