Blog

Script to sort & show directory sizes in Linux

Just change /www/htdocs/ to the desired directory that you want to see the subdirectory sizes for, sorted by size descending.

#!/bin/bash
du -k /www/htdocs/* | sort -nr | awk '
     BEGIN {
 split('KB,MB,GB,TB', Units, ',');
 }
 {
 u = 1;
 while ($1 >= 1024) {
 $1 = $1 / 1024;
 u += 1
 }
 $1 = sprintf('%.1f %s', $1, Units[u]);
 print $0;
 }
 ' > filesize.txt

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>