1

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.

2 Answers 2

2

man du:

   -x, --one-file-system
          skip directories on different file systems
1
  • 1
    but the mount points are directories from the same file system. Commented Jul 30, 2018 at 10:38
1

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).

2
  • is there any other command I could use instead of du? Commented Jul 30, 2018 at 11:53
  • @AliShadloo, see edit Commented Jul 30, 2018 at 12:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.