tux linux devopsOrganizing space in your system can be somewhat trivial, managing different mountpoints, filesystems and physical drives, but linux is ready to help you in this task, with two different commands to check your disk usage.

So if you are here, probably you are interested in knowing how you can check disk usage in linux, here we go!

Introducing your new friends, du and df to check disk usage in linux

Similarly to the basic commands that we discussed in another article (Let’s play with files and directories!), du and df are part of the base package that comes with your distribution, coreutils, and provides you very useful information about your disk usage.

  • du stands for disk usage and the command shows the file space that is allocated for files and directories in a given folder/filesystem.
  • df stands for disk free and the command displays the amount of free space for a given filesystem.

While they could seem similar at a first sight, they give you different information, both useful to troubleshoot disk usage problems.

Let’s see how they work and how they can be used.

df – disk free syntax and options

Below you can find the syntax for the df command is:

Bracketed fields are optional, it means that even in this case you can directly cast the command to get a general output, so if you type:

You get a similar output:

Wait, what? Is this a human language?

As you can see, the base unit of the size is considered in 1k-blocks, making it difficult to read and translate in human language.

Additng the flag -h (human readable) is vital to get a clear output:

What if I want more details?

Fields are self explanatory, but you can decide whether or not to show particular columns of the output using the -o flag.

If you write down the command as is, you will receive the full output of the command, that looks like this:

By specifying the single fields that you want to show, you can just show the columns you are interested into, below you can find the flag syntax, note that the 2 outputs are the same:

This is what you get:

Of course, you can combine the flags to get the information you need in the format you want:

How do I check filesystem types?

Now I can see you, wondering if you can see what kind of filesystem corresponds to the rows shown: yes! With df you can also list the filesystem type, there is a flag that will help you in the process, -T, that will produce this output:

du – disk usage syntax and options

du logic is different from df, as it estimates the space used by files and folders, giving you a fast feedback about what is draining out the space from your drives.

It also shares a similar syntax, but the substantial difference is that you work with files and diectories, rather than filesystems and devices:

Bracketed fields are optional, it means that even in this case you can directly cast the command to get a general output, that is the filesystem space occupied by the current directory and its subdirectories

Let’s then consider a directory that contains another directory and two different files in order to better understand the output:

When you call the command as-is this is the output that you will receive the following output:

You can see that the number near the directory name represents the filesystem space, in 512b blocks, occupied by the directory testdir1 while the last line indicates the TOTAL filesystem space used by:

  • The present directory (identified with a dot )
  • The testdir1 directory (identified as ./testdir1)
  • Both testfile1 and testfile2 (not shown)

Where are my files?

Don’t panic, your files are still there, you need a flag to show them up, because, as I said, the command shows by default the spaced used by directories.

The flag you have to use is -a and it allows you to see the space used by files too:

Wait wait wait.. can you be more human?

Yay, of course, even du command comes to rescue you and provides a flag -h to keep the results human readable:

Combining -h and -a will give you the full output:

What now?

Now you have the key to understand how the commands work, hope this was helpful, if you need some more information about flags that are available in both command, here are th euseful links to the manuals:

DF – manual ~ ~ DU – Manual

If you liked the post, recommend it to your friends and colleagues!