Code Like Clockwork - Elegant, dependable, efficient online services.

How to check directory sizes on the command line

23 Jun 2009

The Unix "ls" command will display Permission and Ownership settings for directories, but not the total size of their contents:

$ ls -lh
total 0
drwxr-xr-x   6 willem  staff   204B Jun  7 00:47 Anime
drwxr-xr-x+  7 nobody  staff   238B Feb  4 11:17 CoD4 Vids
drwxr-xr-x  15 willem  staff   510B Oct 31  2008 Converted for iPod
drwxr-xr-x  23 willem  staff   782B May  8 10:36 Documentaries
drwxr-xr-x   7 willem  staff   238B Jun 11 00:31 Movies & Series
drwxr-xr-x  25 willem  staff   850B Oct 30  2008 TEDTalks - Favourites
drwxr-xr-x  26 willem  staff   884B May 20 01:38 Unconverted

Displaying directory sizes using "du"

To see the total size for directories, you should rather use the "du" command:

$ du -hd 1
572M    ./Anime
196M    ./CoD4 Vids
 88M    ./Converted for iPod
6.8G    ./Documentaries
5.8G    ./Movies & Series
1.4G    ./TEDTalks - Favourites
413M    ./Unconverted
 17G    .

In the above command the "-h" argument specifies that the output should be displayed in human-readable format (using unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte), while the "-d 1" argument specifies that the command should only be executed one directory deep (only for the current directory).

Displaying sorted directory sizes using "du" and "sort"

To sort the output from largest to smallest sizes, you can pipe the output from "du" into the “sort” command:

$ du -d 1 | sort -nr
35441952  .
14242216  ./Documentaries
12127864  ./Movies & Series
2957752   ./TEDTalks - Favourites
1171912   ./Anime
846160    ./Unconverted
401576    ./CoD4 Vids
179336    ./Converted for iPod

In the above command the "-n" argument for "sort" specifies that the results should be sorted numerically, while the "-r" argument specifies that the results should be displayed in reverse (largest to smallest) order.

Displaying sorted and human-readable directory sizes using "du", "sort", and a "while" loop

The output from the last command above isn’t human-readable because "sort" wouldn’t be able to correctly sort output from "du -h" by filesize. To see the total size for directories sorted in descending order, you can pipe the results from “du” into "sort", then pipe that into an inline while loop on a Bash command line:

$ du -kd 1 | sort -nr | while read size fname; do for unit in
  k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e
  "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done;
  done
16G .
6G  ./Documentaries
5G  ./Movies & Series
1G  ./TEDTalks - Favourites
572M    ./Anime
413M    ./Unconverted
196M    ./CoD4 Vids
87M ./Converted for iPod

In the above command, the "-k" argument for "du" specifies that output should be displayed in 1-Kilobyte blocks and the "-d 1" argument specifies that the command should only be executed one directory deep (only for the current directory).

The "-n" argument for "sort" specifies that the results should be sorted numerically, while the "-r" argument specifies that the results should be displayed in reverse (largest to smallest) order.

The inline while loop will read through each result line and convert the raw size value to the proper human-readable output.

Displaying sorted and human-readable directory sizes using an alias

Obviously the above command is too long to comfortably reuse, so you can define an alias for it by adding this line to your ~/.bashrc file on Linux or your ~/.bash_profile file on OS X and restarting your Bash terminal:

alias duf='du -kd 1 | sort -nr | while read size fname; do for unit in
  k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e
  "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done;
  done'

Afterward, simply using the "duf" alias on the command line will present you with the properly sorted and formatted output.

Do you like this? Share it:

Copyright © Geekology 2012. All Rights Reserved.