Makefile Rule to Move Nested Files

So I am trying to write a Makefile to remove files nested in directories with the same names. As an example, I have a directory with two files whose paths are ./1234/1234.txt and ./567/567.txt. I want to move those files up to the top directory so that they are just ./1234.txt and ./567.txt. I tried something like this but it didn't work:

variable = ./1234/1234.txt ./567/567.txt

run: $(variable)

$(variable): $$(notdir %): %
    mv $< $@

It seems like something that would be a common problem but I haven't been able to find an answer.

Thanks so much for the help! :)

1 answer

  • answered 2018-03-14 06:47 user657267

    You have your targets the wrong way round, and trying to use functions in a rule won't work unless you use secondary expansion. You'll also need to conditionally add the dependencies because otherwise your rule will give an error if you've already moved the files.

    variable = ./1234.txt ./567.txt
    run: $(variable)
    $(variable): ./%.txt: $$(wildcard ./$$*/$$*.txt)
        mv $< $@