du: Follow symlinks if symlink points to outside directories, don't follow otherwise

Viewed 6584

I wonder whether there's some rather simple shell incantation to calculate the disk usage of a directory (using the du command) but with the following requirements: symlinks that point to files/subdirectories inside the directory should not be followed, but symlinks to outside files/subdirectories should. Or whether I need to do some scripting to accomplish this.

1 Answers

Unless I have missed something in your question, du already does that when instructed to follow symlinks (-L).

I got two trees:

tree1/:

├── dir1
│   └── file
├── dirA -> ../tree2/dirA
└── link1 -> dir1

and tree2/:

.
└── dirA
    └── file

File is in both cases 1MB in size.

Now run du -shL on tree1/:

$ du -shL tree1
2.0M    tree1

While file can be seen three times in tree1/ when following symlinks:

$ find tree1 -follow -type f
tree1/dirA/file
tree1/link1/file
tree1/dir1/file

It only counted twice towards the total. Namely once from tree1/ and once linked in from tree2/dirA/ (or what I understood as you meaning from outside / not being sub-directories of tree1/).

It would also seem to make sense, because being links inside the same tree, they do not really consume additional disk space (beyond the symlink entry).


I've also grown the tree1 a bit to:

.
├── dir1
│   └── file
├── dir2
│   └── file -> ../dir1/file
├── dirA -> ../tree2/dirA
└── link1 -> dir1

And ran:

$ du -abcL .
1048576 ./dir2/file
1048636 ./dir2
1048576 ./dirA/file
1048636 ./dirA
60      ./link1
2097452 .
2097452 total

As you can see, links only count towards total / size of . with size of link entry (even though I admit 60 confuses me a but, I'd expect 4) and not (dereferenced) content of the file they point to.

Is this what you wanted or close enough?

Related