26

If the wording of the question is wrong, please let me know. It might explain why I can’t find an answer.

I want to find the usage on my main disk using a command like:

du -sh /*

The problem is that I have a number of mount points at the root level, and I would like du to skip these.

I thought the -x option was supposed to do this, but either I misunderstand what it does or I’m using it the wrong way.

How can I apply du to only the root disk without traversing the additional mounts?

Thanks

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Manngo
  • 14,066
  • 10
  • 88
  • 110
  • In other words, are you looking for summary usage of `/bin`, `/etc`, and other folders under `/`, while excluding `/run`, `/proc/`, `/sys`, and so on? – jamieguinan May 17 '16 at 22:44
  • I also have additional drives mounted at `/data` and `/backup` and so on, but that’s basically correct. – Manngo May 17 '16 at 22:47
  • 1
    I think you are using `du` properly, the problem is that `/*` is expanding to include every mount point under `/`, then `du` iterates over each one, which defeats the `-x` usage. The only way to solve this might be with a creative script. – jamieguinan May 17 '16 at 22:54
  • @jamieguinan What would be wrong with replacing `/*` with `/`? Why would that not fix it and instead require a creative script? – Dessa Simpson Mar 29 '18 at 02:20
  • @DuncanXSimpson `/` would only show the sum total of `/`. He wanted to see the subtotals for non-mounted folders. – jamieguinan Mar 29 '18 at 14:55
  • @jamiegunan Oh yeah I wasn't thinking of that. In any case the command they need is `df -h`. – Dessa Simpson Mar 29 '18 at 15:10

2 Answers2

65

du -x will not traverse any mount points it encounters. But if it is told to start at a mount point then it will do as requested.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 2
    To build on this, if you have a volume mounted in the root directory, `du -shx /*` will scan the volume since you are *starting* on the mounted volume. – Dash Jan 22 '21 at 00:20
18

This is hacky, but it seems to do what you want, from the shell,

for d in /*; do egrep " ${d} " /proc/mounts > /dev/null || du -sh ${d}; done

Add a sudo in front of the du if needed.

jamieguinan
  • 1,640
  • 1
  • 10
  • 14
  • 3
    Sorry it’s taken me so long to accept this. Your comment above, I think, explains _why_ the `-x` isn’t working, and your answer is a solution. In my own usage, I have modified it as follows: `for i in /*; do if ! mountpoint -q "$i"; then du -sh $i; fi; done;`. Thanks – Manngo Mar 15 '17 at 00:07
  • 3
    You can avoid the for loop: `du -d1 -x` means starting in the current directory, go down to a depth of 1 (so, single level of subdirectories), and also skip directories on different file systems. My rootfs is an ssd, but also have a zfs raid mounted, and I wanted to do a quick check of what's taking up space on the ssd without wasting time on the hard drives; this works. – ecloud Jan 15 '21 at 06:49
  • but `-s` conficts with `-d1` what was the original question – Hrobky Jul 09 '21 at 16:12