Find all users home directories listed using grep from /etc/passwd

greptext processing

I have a question similar to another one on this site where the individual had to find a list of all users using grep or awk from /etc/passwd. That worked for me but I've tried translating it to find and list the home directories of them also. I already know you can't do it in one line so I know I would use a pipeline. I've done my research online but I can't figure it out the problem is. If I use grep and do something like the following:

   grep -oE '^[/*/]$' /etc/passwd 

…i t would probably give me an error or it will also show me the /bin/bash files which is not what I want. I just need the user names and their home directories listed using grep! I'm also not sure if the * will show other forward-slashes as characters, as some home directories have more than just two /'s (forward-slashes).

Best Answer

Grep is really not the tool for parsing out data this way; grep is more for pattern matching and you're trying to do text-processing.  You would want to use awk.

awk -F":" '$7 == "/bin/false" {print "User: "$1 "Home Dir: "$6}' /etc/passwd
  • awk – The command

  • -F":" – Sets the data delimiter to :

  • $7 == "/bin/false" – Checks if the 7th data column is /bin/false

  • {print "User: "$1 "Home Dir: "$6}' – Says to print the first column and sixth column in the specified format.

  • /etc/passwd – Is the file we're processing

Related Question