LinuxQuestions.org
Help answer threads with 0 replies.
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: Arch
Posts: 2,905

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

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
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: Arch
Posts: 2,905

Original Poster
Rep: Reputation: 77
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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
Old 10-26-2010, 01:43 PM   #6
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Arch
Posts: 2,905

Original Poster
Rep: Reputation: 77
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
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
{ 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: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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

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

All times are GMT -5. The time now is 06:34 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