df vs du for directory sizes

Is it better to use df or du to calculate directory sizes? I tried both and got different numbers with both.

$ du -h /home
1.7G    /home/bob1
1.7G    /home

$ df -h /home
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VG-lv_home
                       25G  1.9G   22G   8% /home

df reports file system disk space usage. Thus, given an argument, FILE, it will report the disk space usage of the filesystem containing FILE. Note that it is not reporting the disk space usage of the directory represented by FILE.

du on the other hand, given an argument FILE, reports the disk space usage of the directory represented by FILE. Presumably, it performs a recursive directory traversal of FILE adding the reported size of each file until the traversal is complete. In contrast with df, du may potentially report cumulative file sizes from files on multiple filesystems.

But, to answer your question more directly--it is best to use du. In many cases (depending on the number of files in the directory represented by FILE), du will take many times longer to execute than df due to the aforementioned directory traversal so expect to wait a little.

du reports usage for the files in directories, while df reports on the filesystem itself. Filesystem overhead like a journal, allocation tables, etc. that take space on the filesystem will show as being used by df, but will not show with du, as they are not userspace accessible. Make a new, empty filesystem and du will should nothing but the cluster(s) for the directory(ies) in it, where df will show only the userspace accessible files on the filesystem.

example of a 2G drive ext3, so journal and lost+found directory:

$df -h /mnt
/dev/sdb1             1.9G   35M  1.8G   2% /mnt
$du -sh /mnt
20K    /mnt

the same with mkfs.vfat (no journal, small allocation table, smaller allocation of for the single directoy):

$df -h /mnt
/dev/sdb1             1.9G  4.0K  1.9G   1% /mnt
$du -sh /mnt
4.0K    /mnt

My guess is that you've got 200M worth of journal and filesystem allocation information, so you show 1.7G on df (1.7 GB of files) and 1.9G on du (1.7 + 200M filesystem stuff)