du and hard links

If, like me, you've ever been curious about if/how coreutil's du command handles hard links:

$ mkdir test
$ mkdir test/a test/b
$ dd if=/dev/zero of=test/a/a bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00215261 s, 487 MB/s
$ dd if=/dev/zero of=test/b/b bs=1M count=2
2+0 records in
2+0 records out
2097152 bytes (2.1 MB, 2.0 MiB) copied, 0.00389201 s, 539 MB/s
$ ln test/b/b test/a/b
$ du -hsc test/a test/b
3.1M    test/a
4.0K    test/b
3.1M    total
$ du -hsc test/b test/a
2.1M    test/b
1.1M    test/a
3.1M    total

So it does handle hard links, but the size of the directories depends on the order in which they are walked.

Indeed, du does have the -l flag which tells it to count hardlinks multiple times:

du -hscl test/a test/b
3.1M    test/a
2.1M    test/b
5.1M    total