#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    56
    Rep Power
    3

    Question Dump All Users To Flat File?


    Is there a way or some kind of simple scripting solution that allows me to take all 400+ users on my Linux (CentOS 5.7) server and dump them out by 'lastname, first' in accordence to their comment in /etc/passwd?

    I need to give a report of all 400+ users to show who has any kind of an account on this server. All my users have a local account and home directory located in /home/$USER/.

    When I look into /etc/passwd, I'm able to see all my users listed by UID:GID but I'd like to pipe them all out based on their 'Last name, First name' which is present in /etc/passwd. I created the accounts using the -c switch which looks like:

    Code:
    usermod -s /bin/false -c "Tony Smith" tsmith
    So now in /etc/passwd, this user has a line as follows:

    Code:
    tsmith:x:1319:100:Tony Smith:/home/tsmith:/bin/false
    Can someone tell me how I can dump all users to a simple / easy to read text file with one user per line sorted by last name, first name?
  2. #2
  3. Learning MySQL, PHP & CSS
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Location
    Powys/Shrops border (UK)
    Posts
    269
    Rep Power
    47
    Hi,

    I've done this by simply importing the /etc/passwd file into MS Excel, delimiting it with space and colon. Then deleting any unwanted rows and columns. A problem would arise if the user had a middle name rather than just forename & surname though.
    Last edited by mikeatvillage; February 22nd, 2012 at 05:28 AM.
    It said 'Insert disk #2', but only one will fit.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,439
    Rep Power
    1688
    I presume you wish to retain all the other info in the passwd file? Also, as Mike says, things will go a little sideways if all the comment fields for the users do not follow the same format. Which, of course, immediately raises the issue of the 'system' users which will certainly not all conform to that standard.
    You could do this, by passing the file through a pipe-line that 'extracts' field 5 (when delimited by colon), performs some specific alterations to it, such as taking first and last space-delimited field, pre-pending them to the line, then passing that on to sort. A bit like (not tested!):
    Code:
    awk -F\: '{words=split($5, name, " "); lname=name[words]; fname=name[1]]; print lname,fname, $0}' /etc/passwd | sort -f -t\: -k 1,2 | cut -d\: -f 3- > name_order_passwd.txt
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo