How can I report on all home directory use?

If you are a system administrator, you can get a CSV listing of all home directory use, including status of the owners of home directories with the following script:

#!/bin/bash

# Read a list of usernames (or directory names)
# and run quota on them.
#
# Falls back to (slow) 'du' if a quota is not set on the user
# or the user id is non-existent.
#
# This is used to get the size of home directories
# of current users, as well as those users who are no longer present.

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

USERNAMES=$SCRIPT_DIR/usernames.txt
QUOTA_CMD="quota"
TARGET_DIR="/mnt/nfs/home"
TARGET_MNT="nfs01-ib:/home"

echo "Username,User_Active,Directory_Present,Directory_Owner,Directory_Group,Space_Calculation_Type,Space_Used,Directory,Dir_Perms"
cat $USERNAMES | while read U
do
        # Is this an active username
        uid=`id -u $U 2>/dev/null`
        if [ "$?" == "0" ]
        then
                user_status="Yes"
        else
                user_status="No"
        fi
        
        # Look up home directory space utilisation
        if [ -d "$TARGET_DIR/$U" ]
        then
                # Get owner and group of home directory
                L=`ls -ld $TARGET_DIR/$U`
                user_owner=`echo $L | awk '{print $3}'`
                user_group=`echo $L | awk '{print $4}'`
                dir_perms=`echo $L | awk '{print $1}'`

                quota_size=`$QUOTA_CMD -u $U 2>/dev/null | grep $TARGET_MNT | awk '{print $2}'`
                if [ "$quota_size" == "" ]
                then
                        # No quota set ... try again with the UID
                        quota_size=`$QUOTA_CMD -u $user_owner 2>/dev/null | grep $TARGET_MNT | awk '{print $2}'`
                        if [ "$quota_size" == "" ]
                        then
                                # no quota set ... should we fall back to 'du' ???
                                quota_size=`du -s "$TARGET_DIR/$U" | awk '{print $1}'`
                                echo "$U,$user_status,Found,$user_owner,$user_group,du,$quota_size,$TARGET_DIR/$U,$dir_perms"
                        else
                                # Found quota by UID
                                quota_size=`echo $quota_size | sed 's/\*//g'`
                                echo "$U,$user_status,Found,$user_owner,$user_group,quota_uid,$quota_size,$TARGET_DIR/$U,$dir_perms"
                        fi
                else
                        # Found quota by username
                        quota_size=`echo $quota_size | sed 's/\*//g'`
                        echo "$U,$user_status,Found,$user_owner,$user_group,quota_username,$quota_size,$TARGET_DIR/$U,$dir_perms"
                fi
        else
                # Unable to find user home directory
                echo "$U,$user_status,Missing,NA,NA,NA,0,$TARGET_DIR/$U,NA"
        fi
done

You will need to set the following variables:

You will need to supply a file usernames.txt which has a list of usernames/home directory names, one per line, e.g.

bob
john
emma
testuser

These will be assumed to be the directories under TARGET_DIR which are then checked, i.e. $TARGET_DIR/bob, $TARGET_DIR/testuser, etc.

This script will not run as a normal user - the calls to quota, lfs quota and du will fail.

Output

The output from the script will be a single line per username/home directory named in usernames.txt, with the following fields:


Back to FAQ