Tuesday, November 11, 2008

Getting Size of Folders in Linux

I often need to know which folders are eating up all my HDD space. So I spent the day pestering the lovely people on irc.freenode.net, and ended up with this bash script:

#!/bin/bash

# DirectorySize : Lists the size of folder and all
# subfolders, biggest first
# Author: Ohmu (sunfish7 dot gmail dot com)
# 11 Nov 08

# Thankyou to
# irc.freenode.net#linux
# irc.freenode.net#awk
# http://awk.freeshell.org/FormatFileSizes
#
# Example use:
#
# spud@spud-laptop:~$ ./dsize
# 4G .
# 3G .local
# 115M .cache
# 3M .thumbnails
# 1M .fr-6Q0eny
# 560K .gnome2
# 46K .purple
#
# TODO:
# Make it operate on a param (eg dsize /tmp)
# Make it recursive. So that if any folder reports more
# than (say) 1Meg, we go into that folder and rinse&repeat
# eg
# $./dsize -depth 0 (infinite) -granularity 1M
# 4G .
# 3G .local
# 2.9G .local/fish
# 3M .local/pudding
# etc
# anyone interested? :) I'm done for now! RSI'd out!
# Ohmu

# Get the byte usage for each subfolder, including .
# Need some tricky syntax to get the .Whatever folders
# Try 'echo {a,b}c' then 'echo {,.}*'
# [!.] takes out . and ..
# We want . so we have to put it in manually
# Sort, largest first
# The awk script replaces eg 4784 with 4K
#

du -s {,.}[!.]* . \
| sort -rn \
| awk '
BEGIN {
u[0]="K"
u[1]="M"
u[2]="G"
}
{
# Get filesize and filename from paramlist
# cannot use $1 and $2 as filename may contain spaces

size=$1 # grab the number
sub(/^[^\t]+\t+/, "") # Remove it (+ spaces) from param list
name=$0 # what is left is the name

for (i=3; i>=0; --i)
{
if ( size > 1024 ^i)
{
# can sub %d with eg %.2f instead for 2dp precision
printf "%d%s \t %s \n", (size / 1024^i), u[i], name
next
}
}
}' \
| more
Just save it as dsize, and chmod u+x dsize, and mv it into some folder bash will find (echo $PATH)

No comments: