command: du

Discussion in 'Beginner Tutorials' started by gcawood, Nov 9, 2011.

  1. gcawood

    gcawood Administrator Staff Member

    Messages:
    49
    Likes Received:
    14
    Trophy Points:
    0
    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.


    USAGE
    The most standard usage is the give `du` either a filename
    Code:
    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.
    Code:
    du -sh [I]directoryname[/I]
    To find out how much space multiple directories are using. For instance, after taking a backup.
    Code:
    du -sh * 
    COMMON FLAGS
    -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)


    Gotcha's
    `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.

    Code:
    #!/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;
    	}
    	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 http://lists.samba.org/archive/rsync/2004-June/009882.html
    Donald W. Grimes likes this.
  2. MustangV10

    MustangV10 New Member

    Messages:
    87
    Likes Received:
    2
    Trophy Points:
    0
    Quite a helpful command, haven't heard of it before.

    Just tried it and it just comes up with a list of file names :S
  3. carbon333

    carbon333 New Member

    Messages:
    87
    Likes Received:
    4
    Trophy Points:
    0
    -c is also useful switch. It returns total amount of data in every file.
  4. lanlanlangzi

    lanlanlangzi New Member

    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
  5. dale

    dale Member

    Messages:
    115
    Likes Received:
    7
    Trophy Points:
    18
    I use this from time to time:
    Code:
    cd /; du -bS|sort -n
    Produces a quick and dirty overview of big files.
  6. Stefano Messicano

    Stefano Messicano New Member

    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    0
    Another example If you want to show the disk usage of a directory you simply put that directory name as the last argument:
    du -h -s movies
  7. sahabcse

    sahabcse New Member

    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I am always using the following option with du

    c s h

    c - Display a grand total
    s - Display an entry for each specified file
    h - "Human-readable" output. Use unit suffixes: Byte, Kilobyte,
    Megabyte, Gigabyte, Terabyte and Petabyte

    Ex)
    vbox:Desktop User$$ pwd
    /Users/users/Desktop
    vbox:Desktop User$$ du -csh ./*
    64K ./1.png
    48K ./10.png
    36K ./11.png
    28K ./12.png
    804K ./13.png
    484K ./14.png
    500K ./15.png
    524K ./16.png
    572K ./17.png
    492K ./18.png
    476K ./19.png
    112K ./2.png
    748K ./20.png
    290M ./21
    92K ./3.png
    84K ./4.png
    108K ./5.png
    116K ./6.png
    124K ./7.png
    108K ./8.png
    152K ./9.png
    1.1M ./Images
    15G ./Desktop
    4.0K ./Network Recording Player
    15G total
  8. Maverick1

    Maverick1 New Member

    Messages:
    24
    Likes Received:
    5
    Trophy Points:
    3
  9. Saptarshi Nag

    Saptarshi Nag New Member

    Messages:
    17
    Likes Received:
    7
    Trophy Points:
    3
    Good one! :)
    Last edited: Feb 11, 2014
  10. rommeloscillada

    rommeloscillada New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
  11. Videodrome

    Videodrome Active Member

    Messages:
    129
    Likes Received:
    44
    Trophy Points:
    28
    I also like to check my memory with df

Share This Page