Show N biggest directories in linux bash

2012-11-01
#du #bash #linux #console #awk #sed

The du (disk usage) utility in Linux bash summarizes the disk usage of each directory or file. By default, it outputs only directories. Just enter some directory and type

1$ du ./

But if ./ has many subdirectories, the output will be too long. And it is unsorted. The task stated in the title can be achieved by piping.

ยง Sort output by size

Cut the first line: it is the size of the ./ directory as a sum of the other sizes. Use --human-readable in du, or divide by 1024 to get KBytes, MBytes, etc. I divide by 1000 to get results close to the Nautilus ones.

The resulting command:

1$ du -b --max-depth=1 | sort -nr | sed -n '2,4p' | awk '{print $1/1000^3" Gb\t" $2}'