command: du


Staff member
Oct 27, 2011
Reaction score
Finding out how much space your files are using in Linux is a bit of a chore compared to the Windows world. (right click, properties) However, Linux comes with a handy tool `du` that will list how much disk space your files occupy.

The most standard usage is the give `du` either a filename
du -ha [I]filename[/I]

Another very popular way to use `du` is to find out how large a given directory is.
The `-s` displays the total space that is used for each directory, but not subdirectories.
du -sh [I]directoryname[/I]

To find out how much space multiple directories are using. For instance, after taking a backup.
du -sh *

-h (make it human readable)
-s (total space in a directory, but do not display the space that subdirectories use.
-a (show how much space a file is using)

`DU` isn't perfect, and if you are a admin long enough, you will find that it doesn't always give you accurate information. Especially if you work with backup archives where one file is hardlinked to another. "Hard Linking" allows one file to exist in multiple locations in a Linux filesystem, but does not duplicate the space requirement. This is a very hand benefit of Linux, especially when you considering backing up data wherever files do not change very often. Using hard links you can have two complete data-sets, but only use the space of one!

Gotcha Example
If you do a `du -sh directory1` and a `du -sh directory2`, where directory2 is a hard link of directory1, it will calculate the space of directory1, twice. Giving you twice as much disk space in use than what is actually being used. The way to get around this is to compare multi-linked inodes by.

Here is a script that I found found handy to use that will calculate the correct space for a directory regardless of whether or not it contains hard links or not.

#!/usr/bin/perl -w

use strict;

my %hash; # Keep track of multi-linked inodes
my $total_blocks = 0;

my @todo = @ARGV;
push(@todo, '.') unless @todo;

foreach my $fn (@todo) {
    my($inode,$nlinks,$blocks) = (lstat($fn))[1,3,12];
    if (-d _) {
	opendir(DP, $fn) or die $!;
	push(@todo, map("$fn/$_", grep(!/^\.\.?$/, readdir(DP))));
	closedir DP;
    } elsif ($nlinks > 1) {
	if (!defined($hash{$inode})) {
	    $hash{$inode} = $nlinks - 1;
	next if --$hash{$inode};
    $total_blocks += $blocks;

print "$total_blocks blocks\n";
print int($total_blocks / 2 + .5), "K\n"; # Assumes 512-byte blocks

You can find more information about this script, it's original author, and the DU and hard links problem at

Members online

Latest posts