Regex – Regex for /etc/passwd Content

bashlinuxpasswdregex

I have a collection files from many systems in my enterprise in a store directory (/store/) and I wanted to find all of the passwd files and cat them out to a single file. Intuitively, the command I came up with and use is:

find /store/ -name passwd -type f  -exec cat {} + > all_passwds.txt

But this also collects the /etc/passwd file that are binary files (I am assuming that some systems symlink to busybox or something). I really just want text-based passwd files.

My next thought is that maybe I could find all /etc/passwd files and pull regex matches. I have been trying for a couple of hours to create a regex that will match the passwd format.

Any help on either creating a regex that will match /etc/passwd files or how to ensure that the command above only grabs text passwd files would be greatly appreciated.

Best Answer

If you want to use a regex, then ^([^:]*:){6}[^:]*$ is probably sufficient to match seven fields seperated by : on each line, so you could do:

find /store -name passwd -type f -exec grep -hIE '^([^:]*:){6}[^:]*$' {} + > all_passwds.txt
  • -h omit the filenames in the output
  • -I skip binary files
  • -E enable extended regular expressions (ERE)