By default, du
will only count each file once if it is linked to multiple times. If you run du -L bar
it will count the file because it only reaches it once. However, if you run du -L *
it will only count it the first time it sees it. For example:
$ du -L foo bar
16K foo
4.0K bar
$ du -L bar foo
16K bar
4.0K foo
Notice that swapping the order of the arguments changes which folder gets reported as 16K.
You can force du
to count the file twice by passing the -l
parameter.
Edit:
Symbolic links are a special kind of file, and an extra step is needed to follow the link. du
will not follow symbolic links unless the -L
option is enabled.
A hard link on the other hand, is basically one file existing in two (or more) folders. du
presumably tracks which files it has seen by inode number to avoid counting these twice. -l
disables this behaviour.
So, with just -L
, it will follow the symbolic link, but if the target file has an inode number it has already seen it will not be re-counted. With just -l
it will count duplicate hard links, but will not follow symbolic links.
If you use -l
and -L
together, it will both follow the symbolic links, and also allow counting the target file(s) more than once.
du
wants to play clever here, but if you rundf -a -L bar foo
the output is very wrong (counting the 16K in bar leaving foo empty).