Script to calculate the file system usage on per user basis.

In this post I am sharing one line useful script to calculate the file system usage on user basis. I am using awk associative array to make my work easy.

Calculating in KB

find ${1:-“/”} -type f -ls | awk ‘ { u[$5]+=$7; g[$6]+=$7 } END { d=1024; for (i in u) { printf “u:%-16s %.2fK\n”,i,u[i]/d } for (i in g) { printf “g:%-16s %.2fK\n”,i,g[i]/d } }’ | sort -nrk2

Calculating in GB

find ${1:-“/”} -type f -ls | awk ‘ { u[$5]+=$7; g[$6]+=$7 } END { d=1048576; for (i in u) { printf “u:%-16s %.2fG\n”,i,u[i]/d } for (i in g) { printf “g:%-16s %.2fG\n”,i,g[i]/d } }’ | sort -nrk2 

You can also put that in file and run by giving the name of file system for which you want to calculate the usage. If file system is not given by default it will run on “/“.

Suppose I am putting this in file name as file1 and making that file executable. Now I can run the script on /tmp file system like below. If nothing provided will run on /

./file1 /tmp

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s