My *nix world

Disk usage in bash

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). So this is how you get disk usage in bash:

#!/bin/bash
#####################################################################
# Script for displaying disk usage
#
# Syntax: du1 FOLDER
#	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 the 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 I think Linux can be fun!

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
Disk usage in bash

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Tagged on: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *