Why do df and du show different output?

When I execute the command df -h /tmp it says disk utilization is 100%, but when it try du -sh /tmp it says disk utilization is 2%.

I want to know why these commands shows different output, how these two commands work and what is the solution to this problem.

Context: /tmp is installed on it own file system. I am using Ubuntu 12.04 server edition on my VPS account. Due to this problem utilization of /tmp 100% according to df some programs complain about free space in /tmp.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

du reports free space by scanning reachable inodes and calculating their size.

If a file is created, opened, and later deleted while it is still open, the program that opened it will continue to have access to the file, and writes and reads can be performed to that memory location on disk. du does not account for such open files whereas df does.

A simple restart should flush /tmp and solve your problem.

An alternative would be to scan open files and restart or kill the process that did not release the deleted files.

Solution 2

Some files on root filesystem can be hidden behind a mount point. Files can be created under the mount point when corresponding device is not mounted, and when device is mounted again, files are not counted by du command. Very probable issue, when there is a big difference between du and df results.

Solution 3

Make sure you sudo when running du. For instance, if you run du -hs /*, you won’t see the real disk usage unless you add sudo.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply