Checking directory sizes on a Bash command line
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.
Related posts:
- Counting files and lines of code in a directory from the command line
- Finding text within specific types of files from the command line
- Reusing commands with different arguments on a Bash command line
- Sending Tweets from the command line using a Bash script
- Editing, Validating and Querying XML with the XMLStarlet command line utility



23 Jun 2009 








author
Very interesting. I’ve always had that chewy problem of sorting the human-readable format, so I am creating that alias right now!
Thanks a lot!
Sure thing! Let me know if you’re able to make any improvements to it, I know that it’s a little slow at calculating large directory sizes at the moment.