LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Delete group users from /etc/passwd SCRIPT (https://www.linuxquestions.org/questions/linux-general-1/delete-group-users-from-etc-passwd-script-299137/)

wesleywest 03-08-2005 09:03 AM

Delete group users from /etc/passwd SCRIPT
 
i'm looking for a script that looks into /etc/passwd and after that he deletes users that are in a specific group.

so lets say group id = 50

then i want all users in /etc/passwd with groupid 50 to be deleted!

is this possible

druuna 03-08-2005 09:31 AM

Hi,

Yes this can be done:

awk -F: '$4 ~ /\<50\>/ { print $1 }' /etc/passwd

The above line will give you all user in the /etc/passwd file that belong to (primary) group 50. You do need the \< and \> around the 50 (word bounderies) otherwise you will also get false hits (500, 650, 50123 etc).

If this is what you want/need you could make this oneliner do all the work (deleting found users) by extending it to:

awk -F: '$4 ~ /\<50\>/ { print $1 }' /etc/passwd | xargs userdell -r

WARNING: Make sure you test this first and understand what is going on!!!! A mistake could render your box useless. WARNING

Hope this helps.

Boow 03-08-2005 09:45 AM

what he said

wesleywest 03-08-2005 09:46 AM

great that helps a lot

but when i execute the line

awk -F: '$4 ~ /\<50\>/ { print $1 }' /etc/passwd | xargs userdel -r

it gives me the output :

usage: userdel [-r] name

druuna 03-08-2005 09:57 AM

Hi again,

My mistake. xargs userdel -r is fed by all the names at ones, it can only handle a single username.

for THIS in `awk -F: '$4 ~ /\<50\>/ { print $1 }' /etc/passwd`
do
userdel -r $THIS
done


The above does work (tested it).

wesleywest 03-08-2005 09:59 AM

thank you so much, this really helped me out reaching my goal for my school project

thnx again


All times are GMT -5. The time now is 01:23 PM.