tidy - remove all files matching a pattern, except the most recently modified file

821 views
Skip to first unread message

Tony Gaetani

unread,
Mar 15, 2016, 11:53:40 AM3/15/16
to Puppet Users
I would like to have a tidy resource that deletes all files recursively from directories matching a pattern, except for the most recently modified file.


Something like this:


tidy {
 
'clean up files except most recently modified one':
    path
=> '/path/to/files',
    recurse
=> true,
    rmdirs
=> true,
    age
=> '1h',
    matches
=> 'some-pattern',
   
unless => '{bash script returning most recently modified file name}',
 
;
}


I understand that this is really stretching the scope of what tidy is for. That is why I have decided to just use an exec with a long line of bash that does what I want. However, this long and complicated line of bash code is not ideal for me because it is hard to understand and maintain, it's just kind of ugly:


# 1 - find all directories matching a pattern, print the most recently modified time and path
# 2 - sort by most recently modified
# 3 - remove the timestamp
# 4 - remove the last item
# 5 - rm -rf all of the paths
find
/path/to/files -maxdepth 1 -type d -name 'some*pattern' -printf '%T+ %p\n' | sort | cut -d' ' -f2 | head -n -1 | xargs rm -rf


Even if tidy is not an option, does anyone else have any better ideas than my exec?

Thanks in advance
-Tony

Carthik Sharma

unread,
Mar 15, 2016, 7:46:53 PM3/15/16
to puppet...@googlegroups.com
Is the most-recently-modified file a resource managed by puppet? If so, you can use tidy with recurse, and tidy will not clean up the managed file.


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/3962a82f-536a-49ef-ad57-ddd872d4ba6f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tony Gaetani

unread,
Mar 16, 2016, 7:32:02 AM3/16/16
to Puppet Users
The most-recently-modified file is not a resource managed by puppet, but I like how you're thinking. Maybe I find a way to configure this file (directory, actually) to be a puppet resource. Thanks for the idea.

jcbollinger

unread,
Mar 16, 2016, 10:47:14 AM3/16/16
to Puppet Users


On Wednesday, March 16, 2016 at 6:32:02 AM UTC-5, Tony Gaetani wrote:
The most-recently-modified file is not a resource managed by puppet, but I like how you're thinking. Maybe I find a way to configure this file (directory, actually) to be a puppet resource. Thanks for the idea.


Having the file you want to retain under management, either directly or via recursive management of its directory, would be a decent direction to go, but it might not be practical.  If it isn't, then there is no resource type among those shipped with Puppet that provides exactly what you want.

If files are reliably added to the target directory on a fixed schedule, however (e.g. old log files produced by logrotate), then you might be able to use the Tidy's 'age' parameter to preserve the most recent one(s).

There is also the option of writing a custom resource type to use in place of Tidy.

On the other hand, maybe it would ease the problem simply to use a cleaner command in your Exec, such as

ls -cd /dir/to/clean/* | grep 'subdir selection pattern' | sed 1d | xargs rm -rf

I guess beauty may be in the eye of the beholder, but I find that one a lot easier to read than yours.  Notes:
  1. The -c option to ls causes the output to be sorted by ctime, from newest to oldest.
  2. If you need extra help to distinguish directories from ordinary files, you could add the -F option to the ls, so that a / will be appended to directory names.  You could then filter out non-directories via the grep pattern (or alternatively, by a slightly longer sed program).  The rm will ignore the trailing slash.

John

Tony Gaetani

unread,
Mar 17, 2016, 5:16:15 AM3/17/16
to Puppet Users
I agree. your command is much more concise and readable than what I originally came up with. Thanks. 
Reply all
Reply to author
Forward
0 new messages