2

I want to know how to see the users by alphabetical order when using this command:

cut -d: -f1 /etc/passwd

I get a list with a lot of names including mine but it's very long. I want to order it alphabetically.

How I can do this? Maybe I'm doing something wrong?

8

Simplest:

cut -d: -f1  /etc/passwd | sort  

To also view the user(s) information from the terminal type:

grep /home /etc/passwd | sort 

The /home tells grep to drop any lines that do not contain the text /home thereby filtering out many lines that don't have information about users from the results of the command. In Ubuntu the user directories are located in the /home directory by default. This command will show several attributes for each user. The user attributes are:

username:password:UID:GID:info:home directory:shell
  • Depending on the situation, one might actually want to use getent passwd instead of the passwd file directly. – muru Mar 25 '15 at 18:58
  • @GilBitton Don't thank people! ;-) If you like the answer, just click the little grey under the abysmally small number now turning it into beautiful green. If you do not like the answer, click on the little grey down-arrow below the number, and if you really like the answer, click on the little grey checkmark and the little up-arrow... ;-) – Fabby Apr 11 '15 at 7:35
6

If you want just to sort only the names (after cut) use Rinzwind's answer.

If you want to sort the /etc/passwd file alphabetically based on the usernames:

sort -t: -k1,1 /etc/passwd 
2

Use the compgen command piped with sort:

compgen -u | sort
0

You could do this through python itself. Just run the below command on terminal.

$ python3 -c 'with open("/etc/passwd") as f:
    for i in sorted([x.split(":")[0] for x in f]):
        print(i)'

Note that the spaces before for was very important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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