Mass Assign Users to Groups
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
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
add a comment |
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
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
linux fedora scripting users group
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 23 at 15:59
jayooinjayooin
3347
3347
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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