df

The df command shows the disk space usage on all the currently mounted file systems. Here is what the output from running the df command without any parameters might looks like. If a file system name is used as an argument (e.g. /dev/sda1) only the information for that file system will be displayed:

The first column “Filesystem” gives the name of the storage (e.g. /dev/sda1) while the second column shows the size of the filesystem in Kilobytes. Likewise the third and fourth columns show how much of the filesystem is used and how much is free while the penultimate column shows the usage as a percentage. The last column “Mounted on” shows the path where the filesystem was mounted. Having the the size of the volume, the amount used and the free space listed in Kilobytes can make the output from df hard to read. Fortunately the “-h” option changes the output into some thing more friendly.

As well as physical hard drives, df also lists other types of mounted filesystem, most notably the udev filesystem for /dev and the tmpfs filesystem for /run and its subdirectories. These are file systems that run in memory and are part of the internal workings of Linux. For example, /run/lock is a place for processes to create lock files (to ensure orderly access to certain resources), for speed they are created in memory rather than on a physical disk. Likewise, the Linux device manager creates the special device files needed by the kernel in /dev directory. Another couple of useful flags for df are “-T” which will add an extra column to the output listing the type of each filesystem, and “-l” which will force df to only show the local filesystems (meaning that remote filesystems mounted via NFS or CIFS won’t be displayed).

du

This Disk Usage command (du) displays how much disk space a directory is occupying. For example, to see how much data is in the “Downloads” directory, type: The output will look something like this:

For a more friendly output use the “-h” option:

The du command will recursively traverse any sub directories and display the amount of space used. The total displayed for any given directory is the space used by the directory itself and any subdirectories. So in the example output above, the Downloads/vendor directory occupies 34 Megabytes, most of which is found under the vendor/qcom/hammerhead/proprietary directory. To find out the total amount of disk space used without seeing the details of the various subdirectories, use the “-s” parameter. Used together with “-h“, the command looks like this: The “-a” parameter displays the size of every file in the directory and its subdirectories. This is useful if you want to find large files. The output from du can be piped into sort to give an ordered list of the files: The “-n” parameter tells the sort command to regard the first column of numbers in the output from du as a numeric string.

Conclusion

The df and du commands can be very useful for monitoring disk usage and for finding directories and/or files which are occupying large amounts of space. As an exercise, see if you can pipe the output of du into sort so that the directories are ordered according to usage.