I've noticed a strange behavior with du
command when it's used with -L
command line option.
I'm using Slackware 14 and Coreutils 8.19.
To reproduce the strange behavior, create two folders:
mkdir foo
mkdir bar
Create a file inside one of the folder:
perl -e 'print "A"x10000' > foo/text
And a symlink in the other folder:
ln -s ../foo/text bar/text
Now, if you type:
du -h -L bar
You'll get:
16k bar
Since the symlink was dereferenced. But if you type:
du -h -L *
You'll get:
16K foo
4.0K bar
And the symlink will not be dereferenced. Am I missing something?
Best Answer
By default,
du
will only count each file once if it is linked to multiple times. If you rundu -L bar
it will count the file because it only reaches it once. However, if you rundu -L *
it will only count it the first time it sees it. For example: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.