I want to calculate a directory size with all its subdirectories. But I had made some of the subdirectories to mount from a mount point. (using mount -B/--bind)

when I use du -hks the returned size includes the mounted directories. Is there a way to eliminate their size from the result?

Edit: The Directories I calculate size are all in the same location. There is a Main directory with shared content for all other directories (This contains all the mount points) and Directories have some individual files. The goal is to calculate the individual files size.


du -x (at least GNU and busybox du) is fooled by Linux bind-mounts because files have the same device-id, so you'd need to prune the mount-points by hand. With GNU du:

du -xhs --exclude=./bind/mount/point

Alternatively, you could use GNU find to find the files and print their disk usage, calling the mountpoint command to know which directories to prune (which are bind-mounts). Then use awk to do the sums (counting hardlinks only once like du does):

find . -xdev ! -name . -type d -exec mountpoint -q {} \; -prune -o \
       -printf '%i %b\n' |
  awk '!seen[$1]++ {s+=$2}
       END{printf "%.17g\n", s * 512}' |
  numfmt --to=iec

That's quite inefficient though in that it needs to run the mountpoint command for each directory (note that it's also possible to bind-mount non-directory files, we're assuming it's not done to avoid running mountpoint on each file).

| improve this answer | |
  • is there any other command I could use instead of du? – Ali Shadloo Jul 30 '18 at 11:53
  • @AliShadloo, see edit – Stéphane Chazelas Jul 30 '18 at 12:15

man du:

   -x, --one-file-system
          skip directories on different file systems
| improve this answer | |
  • but the mount points are directories from the same file system. – Ali Shadloo Jul 30 '18 at 10:38

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.