Thursday, December 13, 2018

du(1) output for just the current directory

I wish this was default behavior of du(1), but it isn't. While working on projects, sometimes I want to find which subdirectory is consuming the most disk space. du is the shell command for this as it computes disk usage per subdirectory starting from the directory you give it, or the current directory if you don't give it a directory name.

But the output leaves a bit to be desired. Typing just 'du' will print a line for each subdirectory from the tree down. This could be several screenfuls of text and you really only want to know what the summary is from the current working directory.

The key the du max depth switch, which is the -d option (or --max-depth=NUM in GNU speak). I do this:

du -d 1

And it just summarizes the subdirectories in the current directory I am in. I usually also use the -h option to get more human-readable sizes.

So now instead of screenfuls of subdirectories nested 47 directories deep, I see something like this: