Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a user-friendly command I can use to list users in a console on an Ubuntu system?

When I cat /etc/passwd I get a hard-to-read list of users. It would be nice to see an alphabetized list, with entries arranged into columns, and with group names in parentheses beside the group IDs.

share|improve this question
2  
well, /etc/passwd is arranged into columns... if you want to see only a few colums, perhaps use cut. For alphabetized, there is sort. If you need the group names, play with join (which may actually be able to show only a subset of columns, btw). –  njsg Feb 3 '13 at 13:15
add comment

4 Answers

up vote 11 down vote accepted

Good way for nice output of /etc/passwd file:

$ column -nts: /etc/passwd

Now you can sort it:

$ column -nts: /etc/passwd | sort

With groups names in last column (no parenthesis):

$ paste -d: /etc/passwd <(groups $(cut -d: -f1 /etc/passwd) | sed 's/.*: //') | column -nts: | sort
share|improve this answer
    
Thanks, these commands do what I want. To a newbie this is very daunting though... I suppose I'll have to learn how to create an alias for them. –  M. Dudley Feb 3 '13 at 17:07
add comment

If you have root access on the machine, you can do the following:

sudo grep -vE '^[^*!]+:[*!]:' /etc/shadow | sort | cut -d: -f1 | while read user; do id $user; done | column -ts' ,' | vi '+set nowrap' -

How it works

Become root to read the shadow file. You only require root privileges if you want to check if the user has a password set (human user), otherwise you can just cat /etc/passwd instead of sudo grep ...:

sudo 

Only show users which have a password set:

grep -vE '^[^*!]+:[*!]:' /etc/shadow

Sort by username:

sort 

Discard all information except for the username:

cut -d: -f1

Iterate through the usernames and enrich it with group infomration:

while read user; do id $user; done

Format the input into columns:

column -ts' ,'

Use vi to view the result:

vi '+set nowrap' - 

If you don't have root access,

try something like this:

cut -d: -f1 /etc/passwd | sort | while read user; do id $user; done | sed 's/\(\()\|^\)[^(]*(\|)\)/ /g' | column -t

Its output is a bit different, but I leave it as an exercise to the reader to combine the two parts in this answer into something that fully fits the job. (Don't you just love sed?)

share|improve this answer
    
"I leave it as an exercise to the reader..." :) –  Emanuel Berg Feb 3 '13 at 21:13
add comment

I thought this would be easy with join, but join requires the files to be sorted on the join field. So it required (?) a workaround with temporary files. The output is sorted on user, and displays user, group, and group ID.

uag () {
  TEMP_GROUPS=/var/tmp/sorted_groups
  TEMP_USERS=/var/tmp/sorted_users
  cat /etc/group  | tr ":" " " | sort -k 3 -o $TEMP_GROUPS
  cat /etc/passwd | tr ":" " " | sort -k 4 -o $TEMP_USERS
  join -1 4 -2 3 -o 1.1,2.1,2.3 $TEMP_USERS $TEMP_GROUPS | sort
  rm $TEMP_GROUPS $TEMP_USERS
}

Translate a character into another with tr; sort according to a key field with -k, output to file with -o; join with regard to fields in the first (-1) and second (-2) file, output certain fields in the first file (-o 1.1) as well as the second (,2.1,2.3).

share|improve this answer
    
note that /tmp would be better, because FHS states that /var/tmp won't be wiped on reboots, which we don't really need. –  strugee Sep 9 '13 at 3:25
add comment

In Ubuntu it can be as follows:

seven fields from /etc/passwd stored in $f1,f2...,$f7

while IFS=: read -r f1 f2 f3 f4 f5 f6 f7
do
 echo "User $f1 use $f7 shell and stores files in $f6 directory."
done < /etc/passwd
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.