LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 09: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, 09:31 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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 09:33 AM.
 
Old 03-08-2005, 09: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 09:46 AM.
 
Old 03-08-2005, 09: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 09:52 AM.
 
Old 03-08-2005, 09:57 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 09: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



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 12:59 AM
/etc/passwd, /etc/shadow, /etc/group? Educate me :)! nutnut Linux - General 4 06-11-2005 07:47 PM
/etc/group - the group users empty Artanicus Linux - General 2 02-22-2005 04:25 AM
restoring passwd/group files jhaiduce Fedora 1 10-20-2004 10:05 AM
passwd group, what else do I need? khutze Linux - Software 0 02-13-2003 05:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:55 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
Open Source Consulting | Domain Registration