Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:23:24+00:00 2026-06-15T12:23:24+00:00

I would like to execute a task in several directories but found no makefile-like

  • 0

I would like to execute a task in several directories but found no “makefile-like” solution up to now. I know this is an often asked question and I know how to solve it for sub-makfiles and so on, but I am looking for something simpler.

Instead of doing

copy: 
     cd module1 && mkdir foo
     cd module2 && mkdir foo
     cd module3 && mkdir foo

I would like to have something like

directories = module1 module2 module3

copy: $(directories)
     cd $< && mkdir foo

but that does not work, since the receipe is called only once with the first directory. I came up with this solution which works but is probably not in the style of Makefiles:

directories = module1 module2 module3

copy: 
     for d in $(directories); do cd $$d && mkdir foo && cd ..; done

How can I do this more nicely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:23:25+00:00Added an answer on June 15, 2026 at 12:23 pm

    There are lots of ways of doing this.

    You can do what Andrew suggests without hard-coding a prefix:

    directories = module1 module2 module2
    
    %/foo: ; mkdir -p -- "$@"
    
    copy: $(addsuffix /foo,$(directories))
    

    which gives

    $ make -n copy
    mkdir -p -- "module1/foo"
    mkdir -p -- "module2/foo"
    mkdir -p -- "module3/foo"
    

    You can also generate the copy target from the makefile:

    directories = module1 module2 module2
    
    define copy-target
      copy:: ; cd $1 && mkdir foo
    endef
    
    $(foreach dir,$(directories),$(eval $(call copy-target,$(dir))))
    

    This gives:

    $ make -n copy
    cd module1 && mkdir foo
    cd module2 && mkdir foo
    cd module3 && mkdir foo
    

    You could also generate the commands, not the target:

    directories = module1 module2 module2
    
    copy: ; $(foreach dir,$(directories),(cd $(dir) && mkdir foo) &&) :
    

    which results in

    $ make -n copy
    (cd module1 && mkdir foo) && (cd module2 && mkdir foo) && (cd module3 && mkdir foo) && :
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to execute code sequentially in an NSOperation. This can be easily
I have a jar file that I would like to execute using PHP, but
I have a specific DB2 query, and I would like to execute this query
I would like to execute a series of commands that compile my program. But
I have read several forums but am seemingly unable to accomplish this simple task.
I would like to terminate all things I've done when i execute a task
I would like to execute a cronjob for a routine task every X hours.
This should be a simple task, but I have seen several attempts on how
I would like to execute some task after a user request in background. My
I would like to execute an Ajax request every seconds. My next code work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.