Search code examples
makefiledirectoryrm

Removing all directories in a path


I have a makefile which lists several directories with which I need to do stuff with, e.g.

DIRS = dir1 dir2 path/to/dir3

all:
    $(foreach DIR,$(DIRS), somecommand --source=$(SOURCEDIR)/$(DIR) --dest=$(DIR);)

clean:
    rm -rf $(DIRS)

Currently the clean target removes dir1, dir2, and dir3, but I would like it to remove dir1, dir2 and path. Something along the lines of:

clean:
    $(foreach DIR,$(DIRS), rm -rf --parents $(DIR);)

Is there an easy way to do this?


Solution

  • There is no such flag available to rm. You could do something like this:

    clean:
            $(foreach DIR,$(DIRS),rm -rf $(firstword $(subst /, ,$(DIR)));)
    

    if you're absolutely sure that you always want to delete everything below the first directory in every path in DIRs (sounds dangerous to me, but...)