Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.