These Unix Spotlights are about some Unix commands that you might not know exist and a few things you can do with them.

TL;DR: du helps you figure out where big files are.


We've all been there.

"Insufficient space?  What does that even mean?  This is a 100gb instance/hard drive!  What could be taking up so much space?"

du is a real easy command to use and a real nice command to know when you're in a space-related pickle.  All you do is type in du — but wait, don't do it yet, there's gonna be a LOT of things scrolling down your screen!  Every single file that's below the directory you're in.  You probably don't want that.  You probably want to group things up a little bit.

Let's look at some of the common flags I use:

  • -c also displays a "grand total" ('c' for cumulative, maybe?)
  • -h, a common option for a few things, that makes the sizes "human readable."  Check it out.
  • -d, arguably the most important one, is depth.  This is essentially our "group by" statement.  -d 0 does the current level only, -d 1 does the grouping at the subdirectory level, etc.  We'll show what this means in a second.

Note that if you get permission errors, you may need to use sudo with this command.

Examples

# Show the current directory's size.
$ du -h -d 0                    
193M	.
193M	total

# Show the size of all the current directory's subdirectories.
▶ du -h -d 1 
178M	./backup_pre_2018
5.9M	./Stream_Images
9.3M	./dogz
193M	.

# Go one level deeper...
$ du -h -d 2
60M	./backup_pre_2018/pictures
178M	./backup_pre_2018
5.9M	./Stream_Images
4.2M	./dogz/n02086079-Pekinese
5.2M	./dogz/n02088094-Afghan_hound
9.3M	./dogz
193M	.

# Sort things from least to most.  Notice that you CAN sort 
# human readable values by using the -h flag with sort.
# -r reverses the sorting.

$ du -h -d 2 | sort -rh
193M	.
178M	./backup_pre_2018
60M	./backup_pre_2018/pictures
9.3M	./dogz
5.9M	./Stream_Images
5.2M	./dogz/n02088094-Afghan_hound
4.2M	./dogz/n02086079-Pekinese

You can do some pretty cool stuff with wildcards and things, but this is pretty much what I use it for.