0

ASK: to use cut command to list all users to confirm a successful useradd.

ISSUE: The list was very long and I had to scroll through output to find user.

NEED: Sort by the date the user was created. Displaying the creation date is not required but a plus.

TRIED:
cut -d : -f 1 /etc/passwd | sort -t

cut -d : -f 1 /etc/passwd | sort -t':' -k2n

cut -d : -f 1 /etc/passwd | ls

3 Answers 3

0

The /etc/passwd file has no date information, so it is unhelpful here.

All non-system users should have a /home/%username% directory. You might try visiting all directories under /home (using a shell loop), and reporting the oldest file or directory in each one. Like:

$ for dir in /home/*; do
    ls -lart --time-style=long-iso "${dir}" |
        grep '^-' | head -n 1 |
        awk '{ printf ("%-10s %s\n", $3, $6); }'
  done
ls: cannot open directory '/home/lost+found': Permission denied
paul       2017-03-16
1001       2021-03-02

If you are fairly lucky, and nobody has back-dated a file with touch, that should show you the user's name (or their numeric uid if they have been removed), and the date of the oldest file in their home directory.

Generally, it is unhealthy to parse the output from ls, but the fields you need for this are fairly predictable.

The order of my output looks odd, because I have a directory /home/viewer (which determines the order of the shell expansion), but user viewer has been removed, and was uid 1001.

I am not sure why you had to scroll through the list to find the user, or why sorting by date would make that easier. You could redirect the output to a file and search with an editor, or grep for the username, or sort the output by the second column.

If you add a user, the traditional method to prove that it worked is to log in as that user, change any default password, fix up any local requirements (like a standard prompt or aliases), check that a few simple commands work, and then ensure the new user sets their own password on their first login.

0

ASK: to use cut command to list all users to confirm a successful useradd.

If you just need to confirm a successful add, you could simply confirm with;

$ cut -d: -f1 <(getent passwd user)

ISSUE: The list was very long and I had to scroll through output to find user.

Just filter the output with any tool you are comfortable with, as above, getent can also do the filtering.

NEED: Sort by the date the user was created. Displaying the creation date is not required but a plus.

If your system has no password policy and the password of the users have not been changed since user set-up, then you can use the chage command to get the date the password was last changed, which would most probably have been when it was created and configured sorting column 2 by month.

cut -d: -f1 <(getent passwd) | while read -r line; do
    c_date=$(sudo chage --list "$line" | awk -F: '/Last password change/{print $NF}')
    printf '%s\t%s\n' "$line" "$c_date"
done | sort -k4n -k3n -k2M
0

cut -d: -f1,3 /etc/passwd | sort -k2n

  • cut -d: -f1,3 /etc/passwd to extract the first field (username) and the third field (date of creation)

  • sort -k2n The output is piped to sort, which sorts the lines based on the second field (date of creation) numerically (-n flag) in ascending order.
    The -k2 option tells sort to use the second field as the sorting key.

This will list the existing users sorted by the date of creation, with the earliest created users appearing first.

You must log in to answer this question.

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