81.1k views
5 votes
List all users (in /etc/passwd) who use either bash (/bin/bash)

or dash (/bin/sh) and their home directory is in /home (second to
last section of : has home directory). Hint: grep twice!

User Jishan
by
8.3k points

1 Answer

1 vote

Final Answer:

To find users with either bash (/bin/bash) or dash (/bin/sh) as their shell and home directories in /home, use the command: `grep -E ':/home/[^:]+:(/bin/bash|/bin/sh)' /etc/passwd`.

Step-by-step explanation:

The command `grep -E ':/home/[^:]+:(/bin/bash|/bin/sh)' /etc/passwd` searches the /etc/passwd file for lines containing '/home' as the second-to-last section (denoting the home directory) and either '/bin/bash' or '/bin/sh' as the shell. Here, `-E` enables extended regular expressions, allowing the use of the `|` operator to match multiple patterns.

The regex pattern `:/home/[^:]+:(/bin/bash|/bin/sh)` specifies the required format for the lines being searched. It looks for entries with '/home' in the second-to-last section and either '/bin/bash' or '/bin/sh' in the shell section.

This command helps filter and display the relevant lines from /etc/passwd, revealing users who fulfill both criteria: having their home directories in /home and using either bash or dash as their shell. The `grep` command efficiently searches and retrieves matching lines, aiding in administrative tasks or user management by providing specific user information based on defined criteria.

User Jas Laferriere
by
8.0k points