31

I'm trying to sort the /etc/passwd numerically by user id numbersb(third field) in ascending order and then send it to s4.

What command would I uses to do that? I'm on this for a while now.

1
  • What have you been trying? It may be useful for someone to explain not only what does work, but also why what you'd tried didn't work. Commented Oct 5, 2018 at 10:26

2 Answers 2

60

Try the below code, Sort the /etc/passwd based on uid.

sort -n -t ':' -k3 /etc/passwd
2
  • Thank you very much for your command. I used a different command, but I think you one will work. Commented Oct 4, 2018 at 11:20
  • consider adding how this command works. Commented Dec 5, 2023 at 6:28
4

There is a specific command for sorting the /etc/passwd file by UID:

# pwck -s

Just to be safe, you should use this instead of generic sort if you're sorting the actual, active /etc/passwd file for your system, as pwck will handle the necessary file locking correctly. Otherwise strange things might happen if someone attempted to log in or even query some user information while you're replacing the /etc/passwd file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.