Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A perl script to find large directories in a given folder UNIX
Here's a little perl script that can be used to find the top ten largest directories from the argument directories. To use it, save it as dirsize in your user's .bin folder (and make it executable with chmod a+x dirsize), modify your .bashrc to add ~/bin to your path, then run something like this in Terminal: dirsize ~/*. Here's the code:

#!/usr/bin/perl
use strict;
die "usage: $0 <directories>\n" unless @ARGV;
@ARGV = map { "'$_'" } @ARGV;
my @results = `du -hs @ARGV`;
@results = sort human_sort @results;
@results = @results[0..9];
print @results;

#---------------------------------------------------------------------------
sub human_sort {
	my ($size_a) = $a =~ /^(\S+)/;
	my ($size_b) = $b =~ /^(\S+)/;

	$size_a = $1 * 1024 if $size_a =~ /^(.*)k$/;
	$size_a = $1 * 1024 * 1024 if $size_a =~ /^(.*)M$/;
	$size_a = $1 * 1024 * 1024 * 1024 if $size_a =~ /^(.*)G$/;

	$size_b = $1 * 1024 if $size_b =~ /^(.*)k$/;
	$size_b = $1 * 1024 * 1024 if $size_b =~ /^(.*)M$/;
	$size_b = $1 * 1024 * 1024 * 1024 if $size_b =~ /^(.*)G$/;

	return $size_b <=> $size_a;
}

[robg adds: I tested this, and it seems to work as expected -- it will take a while to run if you set it to work on a large tree structure.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[10,963 views]  

A perl script to find large directories in a given folder | 9 comments | Create New Account
Click here to return to the 'A perl script to find large directories in a given folder' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A perl script to find large directories in a given folder
Authored by: IslandDan on Sep 14, '07 08:20:19AM

Better yet use the free GUI app that will accomplish the same thing and more: GrandPerspective.



[ Reply to This | # ]
I love GrandPerspective
Authored by: hamarkus on Sep 14, '07 09:37:11AM
For its purpose the pixel representation is really useful. See also:
http://www.dashes.com/anil/2007/07/pixels-are-the-new-pies.html?_

[ Reply to This | # ]
A perl script to find large directories in a given folder
Authored by: tobyvoss on Sep 17, '07 04:53:36AM
GrandPerspective is quite a jewel!
But it really shines when you run it as root using pseudo: http://personalpages.tds.net/~brian_hill/pseudo.html

[ Reply to This | # ]
A perl script to find large directories in a given folder
Authored by: joedec on Sep 14, '07 08:34:40AM
This is great.

Another GUI solution (free) I use is;

http://www.omnigroup.com/applications/omnidisksweeper/

... Jeff

---
Jeff Smith
Sunnyvale, California

[ Reply to This | # ]

OmniDiskSweeper isn't freeware
Authored by: sjk on Sep 16, '07 10:35:14PM

FYI, OmniDiskSweeper is shareware.



[ Reply to This | # ]
A perl script to find large directories in a given folder
Authored by: eisbaer on Sep 14, '07 08:55:38AM

Diskinventory was another free GUI app, but it doesn't seem to be updated recently.

for a quick and dirty CLI hack try
du -ak | sort -nr |less
or
du -ak <directorypath> | sort -nr | less

It gives back a sorted list of the largest directories AND files.
It can take quite some time if you scan a lot of directories and files.
One disadvantage may be the readability: du -ak shows the size in Kbyte; changed to du -ah (h=human) it will return MB and Gbytes, but then the sort command will ignore the units and only sort the numbers.
maybe there is a solution out there ...



[ Reply to This | # ]
A perl script to find large directories in a given folder
Authored by: mibac on Sep 14, '07 12:42:22PM

I had to change
my @results = `du -hs @ARGV`;
to
my @results = `du -h @ARGV`;
in order for it to work on my MacPro 10.4.10



[ Reply to This | # ]
A perl script to find large directories in a given folder
Authored by: JohnDoe55 on Sep 17, '07 10:49:12PM

du -k | sort -n



[ Reply to This | # ]
A perl script to find large directories in a given folder
Authored by: DogDay on Sep 18, '07 02:12:59PM

du -sk * | sort rn



[ Reply to This | # ]