Mass Assign Users to Groups

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















I have 2 text files, users.txt with a list of 1000 users and groups.txt with a list of 50 groups. I want to run a command that adds 30 users to each group (ex: users 1-30 to group 1, users 31-60 to group 2, etc.). What would be the most practical way of doing this?



I suppose I could create 50 new text files with a list of 30 users on each and run 50 bash scripts, but I feel there is a better way than this.



Edit: This is what I have so far:



for i in `cat users.txt` ; do useradd $i; echo Pass$i | passwd $i -- stdin; done
for i in `cat groups.txt ; do groupadd $i; done









share|improve this question



















  • 1





    To do this programmatically you need a user/group mapping. ie. tom:office. Having 50 textfiles each representing one group with the groupname as filename and the contents one user per line could be one option.Give examples of users.txt, groups.txt and how the user/group relation is done between those files to get help.

    – Michael D.
    Jan 23 at 15:17






  • 1





    @MichaelD. It's just an excercise, so the relation is not super important. The users are something like Joe, Sally, Bob, and the groups are something like Arizona, Florida, Utah. I just need each group have 30 users, but I don't have much experience in scripting in Linux.

    – greglorious_85
    Jan 23 at 15:25















1















I have 2 text files, users.txt with a list of 1000 users and groups.txt with a list of 50 groups. I want to run a command that adds 30 users to each group (ex: users 1-30 to group 1, users 31-60 to group 2, etc.). What would be the most practical way of doing this?



I suppose I could create 50 new text files with a list of 30 users on each and run 50 bash scripts, but I feel there is a better way than this.



Edit: This is what I have so far:



for i in `cat users.txt` ; do useradd $i; echo Pass$i | passwd $i -- stdin; done
for i in `cat groups.txt ; do groupadd $i; done









share|improve this question



















  • 1





    To do this programmatically you need a user/group mapping. ie. tom:office. Having 50 textfiles each representing one group with the groupname as filename and the contents one user per line could be one option.Give examples of users.txt, groups.txt and how the user/group relation is done between those files to get help.

    – Michael D.
    Jan 23 at 15:17






  • 1





    @MichaelD. It's just an excercise, so the relation is not super important. The users are something like Joe, Sally, Bob, and the groups are something like Arizona, Florida, Utah. I just need each group have 30 users, but I don't have much experience in scripting in Linux.

    – greglorious_85
    Jan 23 at 15:25













1












1








1








I have 2 text files, users.txt with a list of 1000 users and groups.txt with a list of 50 groups. I want to run a command that adds 30 users to each group (ex: users 1-30 to group 1, users 31-60 to group 2, etc.). What would be the most practical way of doing this?



I suppose I could create 50 new text files with a list of 30 users on each and run 50 bash scripts, but I feel there is a better way than this.



Edit: This is what I have so far:



for i in `cat users.txt` ; do useradd $i; echo Pass$i | passwd $i -- stdin; done
for i in `cat groups.txt ; do groupadd $i; done









share|improve this question
















I have 2 text files, users.txt with a list of 1000 users and groups.txt with a list of 50 groups. I want to run a command that adds 30 users to each group (ex: users 1-30 to group 1, users 31-60 to group 2, etc.). What would be the most practical way of doing this?



I suppose I could create 50 new text files with a list of 30 users on each and run 50 bash scripts, but I feel there is a better way than this.



Edit: This is what I have so far:



for i in `cat users.txt` ; do useradd $i; echo Pass$i | passwd $i -- stdin; done
for i in `cat groups.txt ; do groupadd $i; done






linux fedora scripting users group






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 15:40







greglorious_85

















asked Jan 23 at 14:46









greglorious_85greglorious_85

43




43







  • 1





    To do this programmatically you need a user/group mapping. ie. tom:office. Having 50 textfiles each representing one group with the groupname as filename and the contents one user per line could be one option.Give examples of users.txt, groups.txt and how the user/group relation is done between those files to get help.

    – Michael D.
    Jan 23 at 15:17






  • 1





    @MichaelD. It's just an excercise, so the relation is not super important. The users are something like Joe, Sally, Bob, and the groups are something like Arizona, Florida, Utah. I just need each group have 30 users, but I don't have much experience in scripting in Linux.

    – greglorious_85
    Jan 23 at 15:25












  • 1





    To do this programmatically you need a user/group mapping. ie. tom:office. Having 50 textfiles each representing one group with the groupname as filename and the contents one user per line could be one option.Give examples of users.txt, groups.txt and how the user/group relation is done between those files to get help.

    – Michael D.
    Jan 23 at 15:17






  • 1





    @MichaelD. It's just an excercise, so the relation is not super important. The users are something like Joe, Sally, Bob, and the groups are something like Arizona, Florida, Utah. I just need each group have 30 users, but I don't have much experience in scripting in Linux.

    – greglorious_85
    Jan 23 at 15:25







1




1





To do this programmatically you need a user/group mapping. ie. tom:office. Having 50 textfiles each representing one group with the groupname as filename and the contents one user per line could be one option.Give examples of users.txt, groups.txt and how the user/group relation is done between those files to get help.

– Michael D.
Jan 23 at 15:17





To do this programmatically you need a user/group mapping. ie. tom:office. Having 50 textfiles each representing one group with the groupname as filename and the contents one user per line could be one option.Give examples of users.txt, groups.txt and how the user/group relation is done between those files to get help.

– Michael D.
Jan 23 at 15:17




1




1





@MichaelD. It's just an excercise, so the relation is not super important. The users are something like Joe, Sally, Bob, and the groups are something like Arizona, Florida, Utah. I just need each group have 30 users, but I don't have much experience in scripting in Linux.

– greglorious_85
Jan 23 at 15:25





@MichaelD. It's just an excercise, so the relation is not super important. The users are something like Joe, Sally, Bob, and the groups are something like Arizona, Florida, Utah. I just need each group have 30 users, but I don't have much experience in scripting in Linux.

– greglorious_85
Jan 23 at 15:25










1 Answer
1






active

oldest

votes


















2














Although it is just an exercise and you should do it by yourself, here is a solution:
You really should do that by yourself, or be dumb.



Using some loops and simple tests:



while read group
do
i=30
while read user
do
echo $user:$group
let "i--"
if [ $i -eq 0 ]
then
break
fi
done < /tmp/users.txt
sed -i -e '1,30d' /tmp/users.txt
done < groups.txt > userswithgroup.txt


Note that this will empty your users.txt file. so better make a copy.



Then you still have to add the users; but that's not complicated at this point.






share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496234%2fmass-assign-users-to-groups%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Although it is just an exercise and you should do it by yourself, here is a solution:
    You really should do that by yourself, or be dumb.



    Using some loops and simple tests:



    while read group
    do
    i=30
    while read user
    do
    echo $user:$group
    let "i--"
    if [ $i -eq 0 ]
    then
    break
    fi
    done < /tmp/users.txt
    sed -i -e '1,30d' /tmp/users.txt
    done < groups.txt > userswithgroup.txt


    Note that this will empty your users.txt file. so better make a copy.



    Then you still have to add the users; but that's not complicated at this point.






    share|improve this answer



























      2














      Although it is just an exercise and you should do it by yourself, here is a solution:
      You really should do that by yourself, or be dumb.



      Using some loops and simple tests:



      while read group
      do
      i=30
      while read user
      do
      echo $user:$group
      let "i--"
      if [ $i -eq 0 ]
      then
      break
      fi
      done < /tmp/users.txt
      sed -i -e '1,30d' /tmp/users.txt
      done < groups.txt > userswithgroup.txt


      Note that this will empty your users.txt file. so better make a copy.



      Then you still have to add the users; but that's not complicated at this point.






      share|improve this answer

























        2












        2








        2







        Although it is just an exercise and you should do it by yourself, here is a solution:
        You really should do that by yourself, or be dumb.



        Using some loops and simple tests:



        while read group
        do
        i=30
        while read user
        do
        echo $user:$group
        let "i--"
        if [ $i -eq 0 ]
        then
        break
        fi
        done < /tmp/users.txt
        sed -i -e '1,30d' /tmp/users.txt
        done < groups.txt > userswithgroup.txt


        Note that this will empty your users.txt file. so better make a copy.



        Then you still have to add the users; but that's not complicated at this point.






        share|improve this answer













        Although it is just an exercise and you should do it by yourself, here is a solution:
        You really should do that by yourself, or be dumb.



        Using some loops and simple tests:



        while read group
        do
        i=30
        while read user
        do
        echo $user:$group
        let "i--"
        if [ $i -eq 0 ]
        then
        break
        fi
        done < /tmp/users.txt
        sed -i -e '1,30d' /tmp/users.txt
        done < groups.txt > userswithgroup.txt


        Note that this will empty your users.txt file. so better make a copy.



        Then you still have to add the users; but that's not complicated at this point.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 23 at 15:59









        jayooinjayooin

        3347




        3347



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496234%2fmass-assign-users-to-groups%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay