Delete all subdirectories (recursively), except a few

8 views
Skip to first unread message

Bob

unread,
Apr 30, 2024, 5:46:48 PMApr 30
to help-cfengine
I have to manage a directory that many people can create subdirectories and files. 

Let's say the directory is  "/srv/data". I would like to remove any other directories created (recursively) that are older than 7 days and not is a predefined list.  I can't quite get there.

bundle agent main
{
  vars:
    linux::
      "keep_dirs" slist => { "project1", "project2", "etc" };

  files:
    linux:
      "/srv/data"
       delete => tidy,
        file_select => exclude_name_age_dir("@(keep_dirs)", "7");
         depth_search => recurse("inf");
}
body file_select exclude_name_age_dir(name,days)
{
  leaf_name   => { "$(name)" };
  mtime       => irange(0,ago(0,0,"$(days)",0,0,0));
  file_types  => { "dir" };
  file_result => "file_types.mtime.!leaf_name";
}

This will delete any folders that are empty, but doesn't quite do what I need.

I appreciate any suggestions.


Nick Anderson

unread,
Apr 30, 2024, 6:48:51 PMApr 30
to Bob, help-cfengine
I'd probably consider using find() files to enumerate, possibly filter that list further and then iterate over that list to delete. 

--
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to help-cfengin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/2991fbc6-a151-43ea-aa27-a0d8f20a38fdn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages