explanation about du command linux diskusage

3

What is the difference between these two:

du -h --max-depth=2 /usr/* | sort -nr | head -n 20

And:

du -h --max-depth=2 /usr/ | sort -nr | head -n 20

I would like to display the 20 biggest folder under /usr folder.
Does this 20 folder includes the overall subfolders sizes as well ?

ilansch

Posted 2013-01-16T09:51:34.873

Reputation: 320

Answers

7

There are some important changes in the behavior of these two invocations. Let's use an example, with the structure created by:

mkdir mydir/{.a1,a2,a3}/{.b1,b2}/{.c1,c2} -p

If you invoke du --max-depth=2 mydir you will get:

0       mydir/.a1/.b1
0       mydir/.a1/b2
0       mydir/.a1
0       mydir/a2/.b1
0       mydir/a2/b2
0       mydir/a2
0       mydir/a3/.b1
0       mydir/a3/b2
0       mydir/a3
0       mydir/

But if you run:

du --max-depth=2 mydir/*`

the wildcard character will be expanded and it will become an equivalent of:

du --max-depth=2 mydir/a2 mydir/a3

which will give you the following result:

0       mydir/a2/.b1/.c1
0       mydir/a2/.b1/c2
0       mydir/a2/.b1
0       mydir/a2/b2/.c1
0       mydir/a2/b2/c2
0       mydir/a2/b2
0       mydir/a2
0       mydir/a3/.b1/.c1
0       mydir/a3/.b1/c2
0       mydir/a3/.b1
0       mydir/a3/b2/.c1
0       mydir/a3/b2/c2
0       mydir/a3/b2
0       mydir/a3

The important thing to note here is that it will omit the .a1 directory. In order to include it you would have to run something similar to: du --max-depth=2 mydir/{.[!.]*,*} (but I guess there might be an easier and more generic way that I don't know of). It will also not calculate the overall size of the mydir directory.

And yes, the sizes reported by du include sizes of the subfolders.

konradstrack

Posted 2013-01-16T09:51:34.873

Reputation: 1 171

3

The former counts visible objects within /usr. The latter counts all objects under /usr, including /usr itself.

Ignacio Vazquez-Abrams

Posted 2013-01-16T09:51:34.873

Reputation: 100 516