List usernames sorted by user ids

cut -d ':' -f 1,3 /etc/passwd | sort -t ':' -k2n - | tr ':' '\t'

July 18, 2023harsszeg

Explanation

The cut command extracts fields from each line in the password file:

  • -d ':' means to use : as the field separator
  • -f 1,3 means to extract the 1st and 3rd fields. These correspond to username and user id, respectively

The sort command sorts lines:

  • -t ':' means to use : as the field separator
  • -k2n means to sort by the second field, in numeric order

Finally, the tr command replaces all occurrences of : with a tab character.