System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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...

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...
Ganesan R
Honored Contributor

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

Hi Senthil,

Try this simple for loop to get your desired output.
============================================
for i in `cat /etc/passwd |cut -d: -f6 |grep -i emd`
do
echo `grep $i /etc/passwd |cut -d: -f5`
du -sk $i |awk '{print $1}'
done
============================================
Best wishes,

Ganesh.
Ganesan R
Honored Contributor

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

Hi Again,

If you need the username and home directory size both in same line, use like this.
============================================
for i in `cat /etc/passwd |cut -d: -f6 |grep -i emd`
do
username=`grep $i /etc/passwd |cut -d: -f5`
homespace=`du -sk $i |awk '{print $1}'`
echo "$username \t $homespace"
done
=============================================
Best wishes,

Ganesh.
senthil_kumar_1
Super Advisor

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

Hi James,

Still i am having problem.

pls give me the full script with necessary changes.
James R. Ferguson
Acclaimed Contributor

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

Hi Senthil:

What problems are you having? Does this work for your needs?:

#!/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 "/"
[ "${HOMED}" = "/emd/home" ] || continue
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...
senthil_kumar_1
Super Advisor

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

Hi

I am getting no output.
James R. Ferguson
Acclaimed Contributor

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

Hi (again) Senthil:

> I am getting no output.

Yes, my mistake. You stated that you only want users in the _directory_ '/home/emd'. That is, you want subordinate entries only. If we assume that your directory structure looks like: '/home/emd/user1', '/home/emd/user2', etc. then use this:

#!/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 "/"

[ $(dirname "${HOMED}") = "/home/emd" ] || continue;

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...
James R. Ferguson
Acclaimed Contributor

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

Hi (once more):

Here's a better (more flexible) variation for limiting your output to only users in the '/home/emd' directory. This handles accounts with HOME structures like '/home/emd/user1' and /home/emd/emdusers/user1' whereas my last post won't:

#!/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 "/"

echo "${HOMED}" | awk '/\home\/emd\// {exit 0};{exit 1}'
[ $? = 0 ] || continue;

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...

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

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

My script in the link below can be changed to handle multiple paths:

Either pass them in the command line:
du -sk $* | ...

Or hard code them:
(cd /enmd/home; du -sk *) | ...

You can also change the awk script to only look at the basename of the directory.

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1342062