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 |
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. |
what he said
|
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 |
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). |
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. |