Why df command can return the usage about each partition quickly, while du will take a longer time to compute the disk usage to return.
How does df work?
according to the manuals, df - report file system disk space usage
and, du - estimate file space usage
While df
is to show the file system usage, du
is to report the file space usage. du
works from files while df
works at filesystem level, reporting what the kernel says it has available. Broadly speaking by its design, df
does not care about the files but the filesystem itself.
Working wise, df
Looks at disk used blocks directly in filesystem metadata. Because of this it returns much faster than du
but can only show info about the entire disk/partition. Where as, du
walks through directory tree and counts the sum size of all files therein. It may not output exact information due to the possibility of unreadable files, hardlinks in directory tree, etc.
EDIT
Since you asked this also, how df
works, its really interesting to say that if you use both df
and du
you may see that sometimes there is difference in the amount of free space available. Now you may get surprised thinking how is it possible that two similar commands (df
and du
) returning different free spaces for the same hard disk ???
The answer lies in the working of df
,
As df is related directly to the filesystem metadata, it is also connected to the open file descriptor. Suppose a situation happens, when a file is deleted, it may be possible that some other process is holding the file open, causing it to not be deleted; (restarting or killing that process will release the file) Also if you have created hardlinks, several filenames will point to the same data, and the data (the actual contents) will be marked as free/usable until all references to it has been removed. In this situations, df
will consider the size of those file / data, as a result the available space will be lower.
df
just tells you the total free space on a filesystem. The filesystem can know this instantly, by simple bookkeeping.
du
on the other hand actually traverses a directory and calculates the total size of its contents. This is slower, but it is more powerful too: it can calculate the size of a single directory rather than the entire filesystem, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With