4

I'm trying to replicate a single directory (with sub-directories) to a bunch of new directories based on a list. For example I can:

mkdir Fred Barney Thelma Louise Foo Bar

How would I copy a premade directory (with some empty sub-directories) to the same set of names? For example:

cp -r master_folder/ Fred Barney Thelma Louise Foo Bar

Any suggestions much appreciated!

3

Use a loop to iterate over the directories you want it copied to and copy the contents of your master directory to each one:

dest=(Fred Barney Thelma Louise Foo Bar)
for d in "${dest[@]}"; do
    cp -r master "$d"
done
  • Thank you htor. I seem to get errors when I use the above. If I remove the wildcard after master/ it works but only makes the first folder 'Fred' Tried to vote it up, but need a rep of 15... – user30191 Jan 12 '13 at 20:40
  • @user30191 Fixed that problem. – user13742 Jan 12 '13 at 21:09
3

If your directory names don't contain any special characters (whitespace or \"'):

echo Fred Barney Thelma Louise Foo Bar | xargs -n1 cp -r master_folder

This can also by written:

xargs -n1 cp -r master_folder  < <(echo Fred Barney Thelma Louise Foo Bar)

or more explicitely, through a bash loop (this works for arbitrary file names):

for user in Fred Barney Thelma Louise Foo Bar ;do
    cp -r master_folder "$user"
  done
  • Thank you F.Hauri! That works well. Great site here. Wish I knew a little something something to help in return... Tried to vote it up, but I need a rep of 15... – user30191 Jan 12 '13 at 20:42
  • U could accept this response as you're the asker – F. Hauri Jan 12 '13 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.