LinuxQuestions.org
Visit Jeremy's Blog.
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 02-25-2008, 12:05 PM   #1
kelo81
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Rep: Reputation: 0
How to get the list of groups and users...


Hello everybody,

I'm trying to find a command to get the list of existing groups, and another one to get the list of existing users. Is there any way to do it by using a simple command?.

Also, I know that the id command allows me to get the info of a specifict user, including the groups the user belongs to, but I want to know if there is some kind of command to get the list of users for a specific group (some kind of inverse of id).

Thanks in advance!

Kind regards,

Ezequiel
 
Old 02-25-2008, 12:25 PM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
cat with awk

cat /etc/passwd and cat /etc/group will print out the users and groups respectively, though not elegantly.

cat /etc/passwd | awk -F":" '{print $1}'
cat /etc/group | awk -F":" '{print $1}'

will print just the names of the users and groups, respectively.

I am hoping someone posts an answer involving sed. I looked and could not pull the first match "^.*:" out and just print that.
 
Old 02-25-2008, 12:38 PM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by cmnorton View Post
I am hoping someone posts an answer involving sed. I looked and could not pull the first match "^.*:" out and just print that.
sed 's/\([^:]*\):.*/\1/' /etc/passwd

but I would probably just use cut. cut -d: -f1 /etc/passwd

You don't need to cat something to awk, as awk will take a filename argument. (Known as UUOC or "Useless Use of Cat".)

cut -d: -f1,4 /etc/group might give what you wanted, kelo81, for the groups.
 
Old 02-25-2008, 01:04 PM   #4
kelo81
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Smile It works!!

Well thanks guys!!! All the commands are just working ok!, the cut -d: -f1,4 /etc/group command gives me exactly what I want!

I'm gratefull for your help!

Kind regards,

Ezequiel
 
Old 02-26-2008, 03:01 PM   #5
mtimbro
Member
 
Registered: Feb 2008
Location: Montreal, Canada
Distribution: RedHat 3/4, Ubuntu 7.10
Posts: 86

Rep: Reputation: 15
Quote:
Originally Posted by digiot View Post
sed 's/\([^:]*\):.*/\1/' /etc/passwd

but I would probably just use cut. cut -d: -f1 /etc/passwd

You don't need to cat something to awk, as awk will take a filename argument. (Known as UUOC or "Useless Use of Cat".)

cut -d: -f1,4 /etc/group might give what you wanted, kelo81, for the groups.
You are one serious geek. Props to you.
 
Old 02-27-2008, 12:18 AM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by mtimbro View Post
You are one serious geek. Props to you.
I dunno about that, but thanks.
 
Old 02-28-2008, 06:54 AM   #7
mtimbro
Member
 
Registered: Feb 2008
Location: Montreal, Canada
Distribution: RedHat 3/4, Ubuntu 7.10
Posts: 86

Rep: Reputation: 15
It was mostly for the UUOC statement :-).
 
Old 03-26-2008, 08:58 PM   #8
kelo81
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Hello again! I was using that command for a while (the "cut -d: -f1,4 /etc/group"), but then I realized that it was impossible to use it in real time for an automated script. I mean, it works perfect for the existing users and groups, but if you add some new users with “useradd” (for example), it won’t upgrade the file “/etc/group”!. Even if I modify the default group for a specific user with “usermod -g somegroup”, it won't upgrade the file, it will just leave the old entry.
How can I force the update of the /etc/group file? And iIf it's not possible, is there any other way to query the groups and users for each “group” in real time?
Thanks in advance for your help!!
 
  


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
command to list all groups radiodee1 Linux - Newbie 4 07-09-2007 04:33 PM
What are users and what are groups? harisund Linux - Newbie 5 12-06-2005 12:33 PM
list groups i am a member of curos Linux - Newbie 2 08-11-2005 07:23 PM
What command can list the userids in various user groups? davidas Linux - Newbie 1 04-16-2004 09:01 PM
users and groups krfulton Linux - Security 1 01-05-2004 03:43 AM

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

All times are GMT -5. The time now is 02:42 PM.

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