Linux du command without traversing mounted file systems [closed]
Asked Answered
L

2

29

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

Lifesaving answered 17/5, 2016 at 22:6 Comment(6)
In other words, are you looking for summary usage of /bin, /etc, and other folders under /, while excluding /run, /proc/, /sys, and so on?Miscreance
I also have additional drives mounted at /data and /backup and so on, but that’s basically correct.Lifesaving
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.Miscreance
@Miscreance What would be wrong with replacing /* with /? Why would that not fix it and instead require a creative script?Lysozyme
@DuncanXSimpson / would only show the sum total of /. He wanted to see the subtotals for non-mounted folders.Miscreance
@jamiegunan Oh yeah I wasn't thinking of that. In any case the command they need is df -h.Lysozyme
M
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.

Miscreance answered 17/5, 2016 at 23:1 Comment(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;. ThanksLifesaving
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.Adapt
but -s conficts with -d1 what was the original questionExtinction
E
71

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.

Erythrocyte answered 17/5, 2016 at 22:20 Comment(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.Speos
AKA, do not put the star: du -hx /Prothrombin
M
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.

Miscreance answered 17/5, 2016 at 23:1 Comment(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;. ThanksLifesaving
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.Adapt
but -s conficts with -d1 what was the original questionExtinction

© 2022 - 2024 — McMap. All rights reserved.