HOWTO: Find the Biggest Directories with the du command

The Challenge:

I have a LOT of .mov and .mp4 files, and a lot of work data that I have to move around and transport to and from shows etc.  I need a quick way to see what the various directories contain and to be able to gauge which will fit on a given medium or drive.

I love to use the command line, and I typically have a shell open and ready, so I just want to navigate (using Tab Completion, or course!) to land in a parent directory and find out the sizes of JUST the subdirectories.  I do NOT want to see every bloody file in each one of the subdirectories, and as well, I want to be able to mask out anything that isn’t Gigabytes in size, as it’s too small to matter for my purposes.

Tools we might use:

  • du – The Disk Utilization command, it offers a nice set of options that allow us to view file and directory sizes in raw numbers (aka the kilobytes) or as more human-friendly output, aka “4G”.
  • find – The most powerful tool in the entire Linux operating system distribution, in my humble opinion, and a great way to find files and directories, and get great information about them
  • sort – The sort command allows you to apply various sorting options to output before it shows up on the screen, or to take one file and sort it’s contents and make another file of the desired format of output

First Try

The first one I tried was just running the “du” command in the directory, and that got me this output:



This is kind of what I want, but I find myself counting the decimal places to figure out Megabytes, Kilobytes or Gigabytes, so that’s not quite what I wanted.

So then I tried to add the “-sh” parameters to the command:

du -sh

This gave me the following output:


Definitely not what I wanted, this is way too little info and it’s NOT the space used by each directory, it’s the space used by ALL the directories.

Getting Better Now

Next up I tried adding the “human readable” option and shows things in K/M/G notation, and it shows the size of each of the directories too!

du -h


Find the sizes with find

You can use the find command to get the same sort of information, you just have to format it correctly, like below:

find ./ -maxdepth 1 -type d
Note:  the -maxdepth is how far to go, so a value of 1 means only show 1 directory level deep, what we wanted, and the -type is to show only directories, not regular or other files.

This will bring back the list of subdirectories only, shown below:


Now we need only add the -exec option to make this truly useful:

find ./ -maxdepth 1 -type d -exec du -sh {} \;
Note:  The -exec option will execute the following command “du -sh” on every found line of output, as if were an argument to the du -sh command directly.  The {} brackets are there to show where the found full filenames are to be inserted and the \; is to ensure that the command is run on every single line of output of the find command, til there are no more lines to operate on.

So effectively, it’s as if you had run:

find ./ -maxdepth 1 -type d -exec du -sh Jefferson Airplane Discography
find ./ -maxdepth 1 -type d -exec du -sh Johnny Cash
find ./ -maxdepth 1 -type d -exec du -sh The Essential Marty Robbins_ 1951-1982
find ./ -maxdepth 1 -type d -exec du -sh Western Movies Songs

The returned output will look like this:


Lastly, you can play around by sorting the output using the “sort” command, don’t forget to also use “grep” if you want only to find particular patterns of output in the returned data.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s