du 和 df 的区别


Problem Definition

This section gives the technical explanation of why du and df sometimes report different totals of disk space usage.
When a program that is running in the background writes to a file while the process is running, the file to which this process is writing is deleted. Running df and du shows a discrepancy in the amount of disk space usage. The df command shows a higher value.
Explanation Summary

When you open a file, you get a pointer. Subsequent writes to this file references this file pointer. The write call does not check to see if the file is there or not. It just writes to the specified number of characters starting at a predetermined location. Regardless of whether the file exist or not, disk blocks are used by the write operation.
The df command reports the number of disk blocks used while du goes through the file structure and and reports the number of blocks used by each directory. As far as du is concerned, the file used by the process does not exist, so it does not report blocks used by this phantom file. But df keeps track of disk blocks used, and it reports the blocks used by this phantom file.