#/bin/bash
# run this as root

CHECK_DIRS="/dev /etc /home /opt /usr /var"
DEPTH="3"

# Find files with no user or group attached
/usr/bin/find $CHECK_DIRS -depth -maxdepth $DEPTH -xdev -nouser -nogroup

# This finds users who don't have any files
for u in `/bin/grep -v 'root' /etc/passwd |/bin/cut -d: -f1`
do
  echo -n "$u..."
  x=`/usr/bin/find $CHECK_DIRS -depth -maxdepth $DEPTH -xdev -user $u |wc -l`
  if [ $x -gt 0 ]; then
    echo "cannot be deleted"
  else
    echo "can be deleted"
  fi
done

# Same as above but for groups
for g in `/bin/grep -v 'root' /etc/group |/bin/cut -d: -f1`
do
  echo -n "$g..."
  x=`/usr/bin/find $CHECK_DIRS -depth -maxdepth $DEPTH -xdev -group $g |wc -l`
  if [ $x -gt 0 ]; then
    echo "cannot be deleted"
  else
    echo "can be deleted"
  fi
done