Wednesday, February 8, 2012

Why du and df show different results?

Firstly, let us see how df and du work.

df command makes a filesystem call and gets the details of disk space used directly from the superblocks of the filesystem.

du traverses(walks) the entire filesystem, checking the size of each each file and calculates the disk space used by keeping track of size of each file.

Sometimes df will show more disk space used than du. Why so?

Couple of reasons
  •  Process with open files which are however deleted
  • Running du as non-root user may not allow us to read the directories with restricted read permissions.

Let us examine case 1
Say, suppose a process is writing to a log file and is filling up disk space. To free up disk space we delete that log file to which the process is writing to. Now in this case, du will show that disk space has freed up and will show more free space available. However, df will not show up the freed space and we will instead find that disk space is fast filling up.
When a Linux process opens a file, the reference count to that file is incremented. When a file is removed, though the disk blocks asscoiated with the file gets freed up, the reference to the file opened by the process still exists as the process is still running. Hence, df and du show different disk space usage results. Stopping the process and removing the log file is the right way to do. Then df and du shall show the same disk usage result.

No comments:

Post a Comment