UNIX Disk Usage Command Examples

How do I find out disk usage under UNIX operating systems using GUI, CLI and Perl / shell programs?

You need use the command line utility called du to displays the file system block usage. In this example find out /tmp dir disk usage statistics (open the terminal and type the following command):
$ du /tmp
Sample outputs:

4	/tmp/vmware-root
8	/tmp/pulse-xc7xdoM9vB2K
4	/tmp/.X11-unix
4	/tmp/keyring-7qXGnQ
4	/tmp/.exchange-vivek
4	/tmp/.winbindd
8	/tmp/plugtmp
4	/tmp/virtual-vivek.C81Sd0
4	/tmp/VMwareDnD
4	/tmp/ssh-mhNeIv1961
4	/tmp/.ICE-unix
8	/tmp/orbit-vivek
4	/tmp/.esd-1000
31644	/tmp

The -h option provides “Human-readable” outpu i.e. you will see it in Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte:
$ du -h /tmp
Sample outputs:

4.0K	/tmp/vmware-root
8.0K	/tmp/pulse-xc7xdoM9vB2K
4.0K	/tmp/.X11-unix
4.0K	/tmp/keyring-7qXGnQ
4.0K	/tmp/.exchange-vivek
4.0K	/tmp/.winbindd
8.0K	/tmp/plugtmp
4.0K	/tmp/virtual-vivek.C81Sd0
4.0K	/tmp/VMwareDnD
4.0K	/tmp/ssh-mhNeIv1961
4.0K	/tmp/.ICE-unix
8.0K	/tmp/orbit-vivek
4.0K	/tmp/.esd-1000
33M	/tmp

df: Display Free Disk Space

To show statistics about the amount of free disk space on the specified file system or on the file system of which file is a part use the df command as follows:
$ df
$ df -h

Sample outputs:

Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/wd0a      938M   43.0M    848M     5%    /
/dev/wd0e      817M    2.0K    776M     0%    /home
/dev/wd0d      2.9G    573M    2.2G    20%    /usr

GUI Tools: Disk Usage Analyzer (Gnome Version)

Disk Usage Analyzer is a graphical, menu-driven application to analyze disk usage in any UNIX / Linux / BSD Gnome desktop environment. Disk Usage Analyzer can easily scan either the whole filesystem tree, or a specific user-requested directory branch (local or remote). To start this tool visit Gnome menu click on > Applications > Accessories > Select the Disk Usage Analyzer
Alternatively, you can start Disk Usage Analyzer from a terminal window, just type:
baobab /path/to/dir
baobab /home/vivek/mp3/

Sample outputs:

Fig.01: GUI command to check UNIX / Linux / BSD disk space

Fig.01: GUI command to check UNIX / Linux / BSD disk space

Now, you can:

  • Start a full filesystem scan
  • Select a specific local directory branch to scan
  • Select a remote server and folder to scan etc

To start a full filesystem scan select Analyzer > Scan Filesystem from the menu, or press on the Scan Filesystem toolbar button. Disk Usage Analyzer will display sizes in the directory tree as allocated space. This means that the displayed sizes refer to the actual disk usage and not to the apparent directory size. If you want to view the apparent file size, uncheck View > Allocated Space .

Fig.02: Directory size

Fig.02: Directory size

NCurses Disk Usage

ncdu (NCurses Disk Usage) is a curses-based version of the well-know du, and provides a fast way to see what directories are using your disk space. You can install it as follows under Debian / Ubuntu Linux:
$ sudo apt-get install ncdu
To install the port under FreeBSD, enter::
# cd /usr/ports/sysutils/ncdu/ && make install clean
# pkg_add -r ncdu
Simply type ncdu at shell prompt:
$ ncdu
Sample outputs:

Fig.03: ncdu in action

Fig.03: ncdu in action

Python Sample Code

You can also use python as follows:

import os
size = os.statvfs('/')
output=(size.f_bavail * size.f_frsize) / 1024
print "Available disp space" ,output, "k"

Perl Sample Code

Perl can be also used to find the disk space:

use strict;
use warnings;
use Filesys::DiskSpace;
my $dir = "/home";
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
my $df_free = (($avail) / ($avail+$used)) * 100.0;
my $out = sprintf("Disk space on $dir: %0.2f\n",$df_free);
print $out;

Shell Scripts

You can monitor UNIX / Linux disk space usage with shell script and send an email alert if the percentage of space is <= 95%.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 13 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • Cetka a Tretka Aug 31, 2010 @ 16:45

    You forgot absolutely amazing utility called ncdu (NCurses Disk Usage) [http://dev.yorhel.nl/ncdu].

  • milek Sep 7, 2010 @ 13:24

    And my favorite command in CLI:
    du -h --max-depth=1

    • W1id Sep 8, 2010 @ 11:16

      Mine to. I usually add -x into the mix.

      • Joseph Silva Jan 14, 2012 @ 5:04

        I do something similar on mac os x using
        $ sudo du -h -xd 1
        Takes a while but it’s a very effective way to find out where your space went.

  • Malaya Manas Sep 18, 2010 @ 13:28

    Quickest way to know size of a directory

    du -hs /path/to/filename

    -h hunam readable format
    -s summarize

    It gives a single line output.


  • Icarus Sep 26, 2010 @ 17:22

    Please do not forget to mention that most of those tools are not Unix wide related but Linux specific.

    Example: du -h does not work on HP-UX.

  • Adam Ziaja Aug 14, 2011 @ 18:59

    ls -lahS

  • Siddharth Rana Dec 25, 2011 @ 19:08

    How can we check the size of mounted filesystem size excluding further mounted directories in it ?

    Mount Points :-




    i want to find out size of /abc only excluding further directories .

  • Saleha Jul 19, 2012 @ 18:09

    Please solve my problem:
    In Linux I just want to display the size, Used, Avail, Usage% and mounted info about the file but not the files system. Is there any other command other than df -h to display the data which I am requesting, please respond.

  • watan Jun 15, 2013 @ 12:17

    I have some questions
    1.you have a tab separated file which contains name ,address and phonenumper,list down all phone number without their name and addresses?
    2.how will you find out the total disk usage of the current directory tree..?
    3.how will you count the number of file in your home directory tree( that is ,your home directory and recursive all sub-directories..?

  • Nicolas Jun 27, 2013 @ 11:58

    I recommand to test xdiskusage;
    It is a standard package on most distribution;
    It is perfomant and very visual.


  • pallavi Oct 8, 2015 @ 9:33

    Which command is used to display disk consumption of a specific directory??

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum