Disk usage in bash


Please note that this blog has been moved.

Now it has its own domain: mynixworld.info🙂

If you want to read the latest version of this article (recommended) please click here and I open the page for you.

I run several headless systems and sometimes I need to know the disk usage of a particular directory. We have several choices, from the ls command to the ncurses ncdu utility.

For what I need ‘ls’ does not provide support out of the box. The ncdu looks more alike but I don’t need ncurses dependencies or other tools added to my toolbox. I need a 10 line bash script, eventually written by myself🙂 (let’s reinvent the wheel, don’t we?)

My solution

Create a bash script called “du1” (or whatever you like) with the following content:

#!/bin/bash
#####################################################################
# Script for displaying disk usage
#
# Syntax: du1 FOLDER <FILTER>
#	where FOLDER is the folder to show
#	and FILTER is a float {0..100} that allows you to filter
# 	only those folders which have a usage >= FILTER %
#
# Author        : Eugen Mihailescu
# Last change   : 21.Feb.2013
# E-mail        : eugenmihailescux at gmail dot com
#
# Tested on     : Linux 3.6.10-gentoo-2.1 x86_64 GenuineIntel
#####################################################################

PARENT_DIR=$1
if [ -z $PARENT_DIR ];then
	PARENT_DIR=~/
fi

COLS=$((24+${#PARENT_DIR}))
if [ -n "$2" ];then
	FILTER_SIZE=$2
else
	FILTER_SIZE=0
fi

# print what will be shown
s="Show usage of $PARENT_DIR"
result=`expr $FILTER_SIZE \> 0`
if [ "$result" -eq "1" ]; then
	s="$s, for subfolders usage >= $FILTER_SIZE%"
fi
echo $s

# get the total size of the parent directory
TOTAL=$(du -sB1 $PARENT_DIR 2>/dev/null|cut -f1)

# print each subfolder disk usage
find $PARENT_DIR -mindepth 1 -maxdepth 1 -type d -print0|du -sB1 --files0-from=- 2>/dev/null|sort -gr|awk -v TOTAL=$TOTAL -v fSIZE=$FILTER_SIZE '{bsize=$1;perc=100*bsize/TOTAL;if (perc>=fSIZE){size=bsize; unit="B"; if (size>=1024){size=size/1024;unit="KB";} if (size>=1024){size=size/1024; unit="MB";} if (size>=1024){size=size/1024;unit="GB";} printf("%8.2f %-2s [%-4.1f%%]\t%s\n",size,unit,perc,$2)}}'

# print a subtotal line separator
printf "%${COLS}s\n" |tr " " "-"

# print the total disk usage
echo $TOTAL $PARENT_DIR|awk -v TOTAL=$TOTAL '{bsize=$1;size=bsize; unit="B"; if (size>=1024){size=size/1024;unit="KB";} if (size>=1024){size=size/1024; unit="MB";} if (size>=1024){size=size/1024;unit="GB";} printf("%8.2f %-2s [%-4.1f%%]\t%s\n",size,unit,100*bsize/TOTAL,$2)}'

The result

rpi-gentoo ~ # du1 / 0.1
Show usage of /, for subfolders usage >= 0.1%
    1.12 GB [94.7%]    /usr
   34.38 MB [2.8 %]    /opt
    9.81 MB [0.8 %]    /var
    9.07 MB [0.7 %]    /lib
    6.34 MB [0.5 %]    /bin
    2.88 MB [0.2 %]    /sbin
-------------------------
    1.19 GB [100.0%]    /

That’s why Linux can be fun!

About Eugen Mihailescu

Always looking to learn more about *nix world, about the fundamental concepts of arithmetic, algebra and geometry. I am also passionate about programming, database and systems administration.
This entry was posted in linux, shell and tagged , . Bookmark the permalink.

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