LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 10-22-2010, 07:38 AM   #1
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Debian
Posts: 2,896

Rep: Reputation: 73
Question Create a User List in Alphabetical Order?


I have a server with what appears to have 352 home directories (350 actually if you omit '.' & '..') & I am being asked to basically tally a list of whom all 350 users are in alphabetical order. Now I could sit here for days doing the 'finger' command to obtain their full name commented in '/etc/passwd' file but I would assume there is a script or way I could have Linux quirry the '/etc/passwd' file & take all the user 'comment' entries and export them to a list in alphabetical order. Does anyone know if this could happen and if so, how would I do something like this? I can't write bash / shell scripts to save my life so if anyone could please help me out, I would greatly appreciate this.

Code:
cmennens@mail]:/$ ls -l
total 160
drwxr-xr-x 352 root root 12288 Oct 21 13:41 home
 
Old 10-22-2010, 07:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Looking for this?

awk -F: '{ print $5 }' /etc/passwd | sort > filename

Hope this helps.

EDIT

If you only want the normal users and not the system users:

awk -F: '$3 > 499 { print $5 }' /etc/passwd | sort

Depending on your distro 499 can also be 999

/EDIT

Last edited by druuna; 10-22-2010 at 07:58 AM.
 
1 members found this post helpful.
Old 10-22-2010, 08:01 AM   #3
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,621

Rep: Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518
for i in $(ls /home); do grep "^$i:" /etc/passwd | cut -d: -f 5; done | sort

Do yourself a favor and learn how that works, though.

This snippet looks at all the homedirs and assumes that the homedir is named the same as the username. druuna's reads /etc/passwd directly and doesn't look at homedirs, so use one or the other depending on what you really want.

Last edited by AlucardZero; 10-22-2010 at 08:02 AM.
 
Old 10-22-2010, 08:02 AM   #4
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Debian
Posts: 2,896

Original Poster
Rep: Reputation: 73
Quote:
Originally Posted by druuna View Post
Hi,

Looking for this?

awk -F: '{ print $5 }' /etc/passwd | sort > filename

Hope this helps.

EDIT

If you only want the normal users and not the system users:

awk -F: '$3 > 499 { print $5 }' /etc/passwd | sort

Depending on your distro 499 can also be 999

/EDIT
It's CentOS but yes! Thank you so very very much. Very helpful and thread was rep as so...
 
Old 10-22-2010, 08:13 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You're welcome
 
Old 10-26-2010, 01:43 PM   #6
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Debian
Posts: 2,896

Original Poster
Rep: Reputation: 73
Can someone explain the {print $5} command in the syntax? I can't understand what that does. I am trying to see how I would be able to alter this command to do it by 'last name, first' still being in alphabetical order.

Thanks!
 
Old 10-26-2010, 01:45 PM   #7
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
{ print $5 }

In awk, this prints the 5th field of a record. So, if a record is a line of text, like names, delineated by something (spaces) and has 10 names on the line, each name is a field, hence fields $1 through $10. The whole record is referenced with $0.
 
1 members found this post helpful.
Old 10-26-2010, 02:07 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Only thing that I can add is this: Instead of the default space/tab as delimiter, the /etc/passwd file uses the : That's why the -F: is there, it tells awk to use the : as delimiter.
 
  


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 list the 'chkconfig --list' in alphabetical order? thomas2004ch Linux - Newbie 3 01-08-2010 02:09 AM
Now LQ has so many distributions how about listing them in alphabetical order? catkin LQ Suggestions & Feedback 6 07-01-2009 09:07 AM
Kmenu: alphabetical order? sloteel Linux - Software 3 06-10-2008 04:17 PM
How-to list contents of text file in alphabetical order? sucram2g Linux - Software 1 02-19-2007 03:39 AM
Arranging Files In Alphabetical order swatward Linux - General 4 12-11-2006 08:14 PM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
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