Linux: Directory Utilization with du

  • When working with filesystems, we used the df command. But for files and, more commonly, directories we use the du command instead. The du utility looks at individual files and adds up their collective space used.

    On a single file it is very straightforward:

    $ du mango.png 
    44	mango.png

    Here we see the size of the file in kilobytes. With flags like -h we can do the standard human readable format like we saw with other tools and with -b, -k or -m we can request that all output be in bytes, kilobytes or megabytes.

    Using du we can easily see the size of a folder. This is probably how du is most commonly used. If we use du on the /tmp directory, for example, we will see a list of all of its contents:

    $ du /tmp
    4	/tmp/.X11-unix
    4	/tmp/hsperfdata_mdm
    4	/tmp/Atom Crashes
    4	/tmp/icedteaplugin-mdm-xSGAny
    4	/tmp/ssh-9Dmtg2r6MlLk
    4	/tmp/.ICE-unix
    8	/tmp/mintUpdate
    4	/tmp/pulse-PKdhtXMmr18n
    4	/tmp/.org.chromium.Chromium.4LnxYK
    96	/tmp

    By dafault, when given a directory as input, du is going to list out every visible file in that directory and its size, and then list the directory itself and its total size. If we only want the size of the directory and its contents, not a list of the contents, we add the -s or "summary" flag.

    $ du -s /tmp
    96	/tmp

    This is a handy way to determine how much a directory is holding. When a directory is specified, the volume is automatically calculated recursively to include everything that the directory holds, no matter how deeply it goes.

    You can feed multiple inputs into a single du command or even feed it a wildcard.

    $ du -s /tmp /opt /var /bin /etc
    96	/tmp
    721532	/opt
    2820496	/var
    9936	/bin

    When working with files and directories, du is one of the handiest and most common tools that you will use.

    Part of a series on Linux Systems Administration by Scott Alan Miller

  • I take it there are hidden files then in the /tmp directory? The one listed don't add up to 96.

  • @Dashrender said in Linux: Directory Utilization with du:

    I take it there are hidden files then in the /tmp directory? The one listed don't add up to 96.