LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 03-08-2005, 10:03 AM   #1
wesleywest
Member
 
Registered: Aug 2004
Posts: 43

Rep: Reputation: 15
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
 
Old 03-08-2005, 10:31 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.

Last edited by druuna; 03-08-2005 at 10:33 AM.
 
Old 03-08-2005, 10:45 AM   #3
Boow
Member
 
Registered: Feb 2004
Distribution: Slackware 10.2
Posts: 669

Rep: Reputation: 32
what he said

Last edited by Boow; 03-08-2005 at 10:46 AM.
 
Old 03-08-2005, 10:46 AM   #4
wesleywest
Member
 
Registered: Aug 2004
Posts: 43

Original Poster
Rep: Reputation: 15
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

Last edited by wesleywest; 03-08-2005 at 10:52 AM.
 
Old 03-08-2005, 10:57 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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).
 
Old 03-08-2005, 10:59 AM   #6
wesleywest
Member
 
Registered: Aug 2004
Posts: 43

Original Poster
Rep: Reputation: 15
thank you so much, this really helped me out reaching my goal for my school project

thnx again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort regular users and system users from /etc/passwd joeyBig Red Hat 9 05-29-2008 01:59 AM
/etc/passwd, /etc/shadow, /etc/group? Educate me :)! nutnut Linux - General 4 06-11-2005 08:47 PM
/etc/group - the group users empty Artanicus Linux - General 2 02-22-2005 05:25 AM
restoring passwd/group files jhaiduce Fedora 1 10-20-2004 11:05 AM
passwd group, what else do I need? khutze Linux - Software 0 02-13-2003 06:49 PM


All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration