LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Delete users in bulk (https://www.linuxquestions.org/questions/linux-server-73/delete-users-in-bulk-857115/)

nycblkboy 01-18-2011 04:49 PM

Delete users in bulk
 
Is there a way to remove users in bulk? Maybe by a range of user id numbers.

I help run a school server for our Linux class. I create temp accounts for tests and now I just want to delete them. I really should delete the old users too but the Professer likes to let them have access even after the class.

zer0signal 01-18-2011 06:29 PM

Maybe use some type of script to take input from you about who you want to useradd -m "username" into the system while taking that variable that was used for the input and redirecting it to another file >> users.txt That is to be read later when making a script to remove userdel -f < /location/users.txt

obviously ran as either sudo or root.

the quickest idea I can come up with...

nycblkboy 01-18-2011 06:45 PM

Quote:

Originally Posted by zer0signal (Post 4229382)
Maybe use some type of script to take input from you about who you want to useradd -m "username" into the system while taking that variable that was used for the input and redirecting it to another file >> users.txt That is to be read later when making a script to remove userdel -f < /location/users.txt

obviously ran as either sudo or root.

the quickest idea I can come up with...

Good idea i'm going to add a few accounts and try it.
Do you know if userdel -f xkcd* would work? Where xkcd* is a list of users whose username begins with xkcd.

zer0signal 01-18-2011 06:54 PM

I don't think you can use the wildcard like that. =(
But I am not a great scripter, where I would look is into into a loop function for userdel, that would just keep that script running till it reached the end of user.txt.

frndrfoe 01-18-2011 07:46 PM

You could build the list from /etc/passwd
Code:

awk -F":" '{ print $1 }' /etc/passwd |grep '^xkcd' >users.txt
or from a directory service
Code:

getent passwd | awk -F":" '{ print $1 }' | grep '^xkcd' >users.txt

zer0signal 01-18-2011 07:58 PM

nice! I need to play around with awk and sed more! thx for the info frndrfoe!

nycblkboy 01-18-2011 08:05 PM

Quote:

Originally Posted by frndrfoe (Post 4229435)
You could build the list from /etc/passwd
Code:

awk -F":" '{ print $1 }' /etc/passwd |grep '^xkcd' >users.txt
or from a directory service
Code:

getent passwd | awk -F":" '{ print $1 }' | grep '^xkcd' >users.txt

Buliding the list not be a problem. I can just take the passwd file import it into excel and erase the info i don't need and save it as text. But thanks I keep forgetting how powerful Unix is.


All times are GMT -5. The time now is 09:38 AM.