Results 1 to 7 of 7

Thread: command: du

  1. #1
    Join Date
    Oct 2011
    Posts
    46
    Thanks
    0
    Thanked 7 Times in 6 Posts
    Rep Power
    10

    command: du

    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 filename
    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 directoryname
    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...ne/009882.html




  2. #2
    Join Date
    Nov 2011
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    2
    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. #3
    Join Date
    Feb 2012
    Posts
    90
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    2
    -c is also useful switch. It returns total amount of data in every file.

  4. #4
    Join Date
    Mar 2012
    Location
    Chongqing China
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks.

  5. #5
    Join Date
    Mar 2012
    Location
    Cardboard box
    Posts
    113
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Rep Power
    2
    I use this from time to time:
    Code:
    cd /; du -bS|sort -n
    Produces a quick and dirty overview of big files.
    distrACT -- an open community

  6. #6
    Join Date
    Apr 2012
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    2
    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. #7
    Join Date
    Nov 2012
    Location
    Colachel
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    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)
    vboxesktop User$$ pwd
    /Users/users/Desktop
    vboxesktop 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

 

 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
           








Check out Linux Central for Linux software and other goodies!





» Stats

Members: 3,563
Threads: 3,917
Posts: 9,436
Top Poster: Fred (1,486)
Welcome to our newest member, nahidworld

» Links



Powered by vBadvanced CMPS