Tidy up dir recursively

25 views
Skip to first unread message

Christian Linden

unread,
Oct 13, 2016, 12:33:52 PM10/13/16
to help-cfengine
Hi,

how do I rm -rf on a dir:

 files:
          comp_exists::
            "/path/to/my/dir/."
              delete => tidy,
              depth_search => recurse("inf"),
                classes => if_ok("removed");

This results in:  Dangerous or ambiguous promise - '/path/to/my/dir/.' specifies recursive deletion but has no file selection criteria

c

Nick Anderson

unread,
Oct 13, 2016, 12:38:40 PM10/13/16
to Christian Linden, help-cfengine

Christian Linden writes:

> Hi,
>
> how do I rm -rf on a dir:

There are utility bundles rm_rf(name), and rm_rf_depth(name, depth) in the
standard library.

bundle agent rm_rf(name)
# @brief recursively remove `name` to any depth, including base
# @depends rm_rf_depth
# @param name the file or directory name
#
# This bundle will remove `name` to any depth, including `name` itself.
#
# **Example:**
#
# ```cf3
# methods:
#     "bye" usebundle => rm_rf("/var/tmp/oldstuff");
# ```
{
  methods:
      "rm" usebundle => rm_rf_depth($(name),"inf");

}

bundle agent rm_rf_depth(name,depth)
# @brief recursively remove `name` to depth `depth`, including base
# @depends recurse_with_base tidy all
# @param name the file or directory name
# @param depth how far to descend
#
# This bundle will remove `name` to depth `depth`, including `name` itself.
#
# **Example:**
#
# ```cf3
# methods:
#     "bye" usebundle => rm_rf_depth("/var/tmp/oldstuff", "100");
# ```
{
  classes:
      "isdir" expression => isdir($(name));
  files:
    isdir::
      "$(name)"
      file_select => all,
      depth_search => recurse_with_base($(depth)),
      delete => tidy;

      "$(name)/."
        delete => tidy;

    !isdir::
      "$(name)" delete => tidy;
}

Neil Watson

unread,
Oct 13, 2016, 12:42:05 PM10/13/16
to help-cfengine

Christian Linden

unread,
Oct 13, 2016, 12:53:02 PM10/13/16
to Neil Watson, help-cfengine
alright.. did that with commands: now =)

Thanks!
c
> --
> You received this message because you are subscribed to a topic in the Google Groups "help-cfengine" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/help-cfengine/5UW_nZY3NS0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to help-cfengin...@googlegroups.com.
> To post to this group, send email to help-c...@googlegroups.com.
> Visit this group at https://groups.google.com/group/help-cfengine.
> For more options, visit https://groups.google.com/d/optout.

Christian Linden

unread,
Oct 13, 2016, 12:58:47 PM10/13/16
to help-cfengine, cfen...@watson-wilson.ca
next prob:

classes:
          "comp_exists" expression => fileexists("/path/to/my/dir");

The class is set no matters if dir is a dir or a file =(

c

Neil Watson

unread,
Oct 13, 2016, 1:03:19 PM10/13/16
to help-cfengine
On Thu, Oct 13, 2016 at 09:58:47AM -0700, Christian Linden wrote:
> The class is set no matters if dir is a dir or a file =(
classes:
          "comp_exists" expression => fileexists("/path/to/my/file");
          "comp_exists" expression => fileexists("/path/to/my/dir/.");

or
          "comp_exists" expression => fileexists("/path/to/my/file");
          "comp_exists" expression => isdir("/path/to/my/dir");

Christian Linden

unread,
Oct 13, 2016, 1:20:35 PM10/13/16
to Neil Watson, help-cfengine
isdir is worth a try.
> "comp_exists" expression => fileexists("/path/to/my/file“);
>> this sets class if file is a dir as well.

Thanks, Neil, I’m out for 2day.
Chris

Ted Zlatanov

unread,
Oct 13, 2016, 4:45:32 PM10/13/16
to help-c...@googlegroups.com
On Thu, 13 Oct 2016 09:33:51 -0700 (PDT) Christian Linden <lindo...@gmail.com> wrote:

CL> how do I rm -rf on a dir:

CL> files:
CL> comp_exists::
CL> "/path/to/my/dir/."
CL> delete => tidy,
CL> depth_search => recurse("inf"),
CL> classes => if_ok("removed");

CL> This results in: Dangerous or ambiguous promise - '/path/to/my/dir/.'
CL> specifies recursive deletion but has no file selection criteria

Please use
https://docs.cfengine.com/docs/master/reference-standard-library-common.html#rm_rf
or
https://docs.cfengine.com/docs/master/reference-standard-library-common.html#rm_rf_depth
in the standard library. Both have examples of how to use them. IMHO
that's significantly easier than learning all the attributes and bodies
you need for such a simple task.

Ted


Christian Linden

unread,
Oct 14, 2016, 8:15:32 AM10/14/16
to help-cfengine
cool, Ted, thank you =)

Chris
Reply all
Reply to author
Forward
0 new messages