11

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?

  • it seems du wants to play clever here, but if you run df -a -L bar foo the output is very wrong (counting the 16K in bar leaving foo empty). – guido Jul 10 '13 at 10:48
18

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.

|improve this answer|||||
  • Thank you. This works. However I would love to have some extra explanation. On man page, I read that -l does count sizes many times if hard linked, but here I'm working with symlink. Moreover, -l alone doesn't solve the problem, while if I use both -l and -L it counts the size properly. – Zagorax Jul 16 '13 at 12:49
  • @Zagorax Yeah, the man page is a little unclear on how -l and -L interact. The explanation got a bit too long for a comment, so I've updated my answer. – David Baggerman Jul 16 '13 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.