Understanding the Linux df And du Commands

Spread the love

Although it is possible to get information about disk usage from within the various Linux desktops, those who are comfortable with the command line can get much greater detail using the df and du commands. With these two commands, not only can you discover details about the free space on the mounted file systems, but you can also see the amount of space used by individual directories.

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.

df -h

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:

du Downloads

The output will look something like this:

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

du -h Downloads

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:

du -hs Downloads

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:

du -a Downloads/ | sort -n

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Gary Sims

Gary has been a technical writer, author and blogger since 2003. He is an expert in open source systems (including Linux), system administration, system security and networking protocols. He also knows several programming languages, as he was previously a software engineer for 10 years. He has a Bachelor of Science in business information systems from a UK University.

Comments (2)