unix home directories without entries in /etc/passwd unix home directories without entries in /etc/passwd shell shell

unix home directories without entries in /etc/passwd


This will report all home directories from /etc/passwd that should be in /home but aren't:

cut -d":" -f6 /etc/passwd | grep home | sort |     while read dir; do [ -e "$dir" ] || echo Missing $dir; done

And this one reports all that don't exist:

cut -d":" -f6 /etc/passwd | while read dir; do     [ -e "$dir" ] || echo Missing $dirdone


as 1st approximation:

perl -F: -lane 'next if m/^#/;print "$F[5] for user $F[0] missing\n" unless(-d $F[5])' /etc/passwd

if you want find the differences between the /etc/passwd and the /home

comm <(find /home -type d -maxdepth 1 -mindepth 1 -print|sort) <(grep -v '^#' /etc/passwd  | cut -d: -f6| grep '/home' | sort)

in an narrow form

comm    <(            find /home -type d -maxdepth 1 -mindepth 1 -print |sort        ) <(            grep -v '^#' /etc/passwd  |cut -d: -f6 |grep /home |sort        )

if you will use

  • comm ... (without args as above) will show 3 colums 1.) only in /home 2.)only in /etc/passwd 3.) common
  • comm -23 .... - will show directories what are only in the /home (and not in the /etc/passwd)
  • comm -13 .... - will show dirs what are only in the /etc/passwd and not in the /home
  • comm -12 .... - will show correct directories (exists in the /etc/passwd and the /home too)

I'm not sure with the -{max|min}depth on the AIX..


So, assuming you want to know if there are directories under /home which do not correspond to existing users:

for name in /home/*; do    if [ -d "$name" ]; then        cut -d':' -f6 /etc/passwd | egrep -q "^$name$"        if [ $? -ne 0 ]; then            echo "directory $name does not correspond to a valid user"        fi    fidone

Then again, this assumes you are not using a name service such as LDAP or NIS, in which case, change the line starting with cut to:

getent passwd | cut -d':' -f6 | egrep -q "^$name$"