Sort Your Password File by User ID (UID)

Your /etc/passwd file can sometimes become disorderly as users come and go on a system. This simple script displays the file sorted numerically by the uid field.

sort -t : -k 3,4 -g /etc/passwd

The -t option identifies the field separator. In this case we use the colon (:). The -k 3,4 option determines the order in which fields will be compared for the sort. We're sorting by the 3rd field (UID) followed by the 4th field (GID). Lastly, the -g option means we want the sort to occur numerically. This makes the number 56 appear after 9. Using the default (ASCII) sort places 56 before 9 (5 comes before 9).

This variation sorts the group file by the group id (GID) field.

sort -t : -k 3 -g /etc/group

Leave a comment


Type the characters you see in the picture above.

Who's this guy?

Aaron Longwell is Chief Web Craftsman at New Media Logic Corporation in Coeur d' Alene, Idaho. As a professional software developer for 12 years and a student of public policy, he occasionally has interesting things to say about software, technology, culture and politics.

Subscribe to feed Subscribe to my RSS Feed

  • View Aaron Longwell's profile on LinkedIn
  • Recommend Me