Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's assume I have a Linux box configured as a primary domain controller with 50 users connecting at any given time. If I wanted to create two groups, one called "Teachers" and the other "Students" How could I script something so I wouldn't have to individually type out each command for each user when adding them to their appropriate group?

This is what scripting is all about yes?

share|improve this question
up vote 3 down vote accepted

A loop in this format will work:

while read i ; do 
    command_name "$i"
done < filename

The file named filename would contain a list of users, one per line. For the command name command_name use useradd -G {group-name}.

share|improve this answer

Change group1 and group2 to your desired groups, and use -M user1,user2,user3,... as the users.

for group in group1 group2
do gpasswd -M user1,user2,... $group
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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