Operating System - HP-UX
1753461 Members
4916 Online
108794 Solutions
New Discussion юеВ

Re: How to view all the users home directory size.

 
SOLVED
Go to solution
senthil_kumar_1
Super Advisor

How to view all the users home directory size.

Actually we can see all the users home directory size using bellow command

# du -sk /home/*

343608 kchestnu/
47808 ke5qzf/
56 ke65a7/
16 ke980y/
1184 ke9isi/
201736 keane/
440 kegkmr/
161792 kets9t/
48 keyma9/


These names are the userid given for users.

Normally we are mentioning the usernames as comment when adding users.

Ex:

# vi /etc/passwd

root@lgapps:/emd/home > grep kchestnu /etc/passwd
kchestnu:blmfFX7fqGWms:1440:1003:Kenneth Chestnutt,,,:/emd/home/kchestnu:/usr/bin/sh


My customer wants a report based on Full username (comment name given in /etc/passwd file.)

is it possible.

how to get this.

17 REPLIES 17
Mel Burslan
Honored Contributor

Re: How to view all the users home directory size.

The given name of the user is contained in a field called gecos in the password file.

If all users are created using SAM Or a consistent set of scripts, this command will give you the actual user name:


cat /etc/passwd |while read line
do
uname=`echo $line|cut -d: -f1`
givenname=`echo $line|cut -d: -f5|cut -d"," -f1`
echo "$uname --> $givenname"
done

But again, since you can alter the gecos field with no effect on system performance, this might not work for all users. Use it at your own risk.

Hope this helps
________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor
Solution

Re: How to view all the users home directory size.

or for your particular case:

cd /home
du -sk * | while read line
do
space=`echo $line | awk {'print $1'}`
uname=`echo $line |awk {'print $2'}
givenname=`grep ^${uname} /etc/passwd|cut -d: -f5|cut -d"," -f1`
echo "$space \t $givenname"
done
________________________________
UNIX because I majored in cryptology...
Patrick Wallek
Honored Contributor

Re: How to view all the users home directory size.

Try this:

# cat home-sizes.sh
#!/usr/bin/sh

export IFS=:

while read USER PASS UID GID GECOS HOMED SHELL
do
SIZE=$(du -ks ${HOMED})
echo "Size of ${GECOS} home directory - ${SIZE}"
done < /etc/passwd
Mel Burslan
Honored Contributor

Re: How to view all the users home directory size.

on my second posting, I forgot a back tick at the end of the line starting with uname. please put a ` sign at the end of that line before trying this script.

(no points for this post please)
________________________________
UNIX because I majored in cryptology...
senthil_kumar_1
Super Advisor

Re: How to view all the users home directory size.

Hi Mel Burslan,

Your second script is what i want.

Now i want one more thing to be added.

that some of my home folder is linked from another folder.

Ex:

root@lgapps:/home > ll -nd chobot
lrwxr-xr-x 1 0 3 18 Oct 20 2008 chobot -> /home/chobot


I want to add this link in fourth column.

is it possbile?

Now the is as following as per your script.

Ex:

208 xz4d64 S. Ostap
208 tzd33d Joe Hochstenbach
208 praval Pravin Raval


Now i want to add fourth column for linked directory if any.

James R. Ferguson
Acclaimed Contributor

Re: How to view all the users home directory size.

Hi Senthil:

You could do this to show the GECOS name and the HOME directory with any symbolic linkage:

# cat ./showids
#!/usr/bin/sh
OLDIFS=${IFS}
IFS=":"
while read USER PASS UID GID GECOS HOMED SHELL X
do
[ "${UID}" -le 100 ] && continue #...skip system accounts
[ "${HOMED}" = "/" ] && continue #...skip "/"

if [ -d "${HOMED}" ]; then
DIR=$(ls -ld ${HOMED} | awk '{if (NF>9) {print $9,$10,$11} else {print $9}}')
else
continue #...skip missing directories
fi
SIZE=$(du -ks ${HOMED} | awk '{print $1}')
printf "%6s %-8s '%-30s' [%s]\n" ${SIZE} ${USER} ${GECOS} ${DIR}
done < /etc/passwd
IFS=${OLDIFS}
exit 0

...

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to view all the users home directory size.

Someone else asked the same question in a Linux forum, see my reply:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1342062
senthil_kumar_1
Super Advisor

Re: How to view all the users home directory size.

Hi James,

Actually we have created home directories for users in two different locations.

Some users are having their home directory in - /emd/home

And some others are having their home directory in - /eng/home.


Here i want to take the report of /emd/home only.


I am not capable to change your script.

So pls do make necessary changes.

James R. Ferguson
Acclaimed Contributor

Re: How to view all the users home directory size.

Hi (again) Senthil:

> Here i want to take the report of /emd/home only. I am not capable to change your script.

You need to LEARN to be able to modify and create scripts to be a Unix administrator!

Look again at the script I offered you. Notice the line:

"${HOMED}" = "/" ] && continue #...skip "/"

This says to continue (or resume looping) if the ${HOMED} directory is a "/". The '&&" means if the test in square brackets is true, then execute what follows the '&&'.

From this you would know that you could add another line, like:

[ "${HOMED}" = "/emd/home" ] || continue

This variation says to coninue looping if the tested condition is NOT TRUE --- just what you want.

You could have written the last statement as:

if [ "${HOMED}" != "/emd/home" ]; then
continue
fi

...but that's a bit longer.

Regards!

...JRF...