3

I want to create a script that change a username. I want to check if the user is not a system user. My idea is to check /etc/passwd and pick only users with an ID between 1000 and 60000 and users that have a /home directory like

user:x:1005:1021::/home/user:/bin/sh

My grep command for now is like

egrep -E '1[0-9]{3}.*/home' /etc/passwd 

As you can see, it doesn't match my [1000-60000] pattern nor the name

1
  • 1
    Minor point: grep -E is the same thing as egrep. There's no point in using both (egrep -E), just use grep -E.
    – terdon
    Jul 17, 2019 at 14:47

1 Answer 1

6

You can use awk to find local users with id >= 1000:

awk -F: '$3>=1000 && $3!=65534 {print $1}' /etc/passwd

To also check for a home folder below /home:

awk -F: '$3>=1000 && $3 != 65534 && $6 ~ /^\/home/ {print $1}' /etc/passwd

Replace /etc/passwd with <(getent passwd) to list all users including network accounts ...

See also this related question, as well as this one.

3
  • Thanks! but how can I merge all my patterns e.g name + ID + /home?
    – danaso
    Jul 17, 2019 at 14:30
  • 2
    Mind that this does not list LDAP users so is only useable for local users :)
    – Rinzwind
    Jul 17, 2019 at 14:34
  • Thanks @Rinzwind. I didn't know
    – danaso
    Jul 17, 2019 at 14:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .