Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Newbie: Assessing Disk Usage

Newbie: Assessing Disk Usage
Thread Tools
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
Jun 30, 2005, 08:53 PM
 
My hard drive is quite full and I'm not sure why. Accordingly, I'd like to display the top, say, 30 largest files on my hard drive in descending order by size. I'm figuring on using the "du" command in the Terminal but I'm finding the precise syntax a little hard to glean from the man pages.

Anyone know how to form this command?

EDIT: I found a good link here that helps a lot. Thought I'd let you know how this issue progressed for me. Thanks!
(Last edited by selowitch; Jun 30, 2005 at 10:00 PM. )
     
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
Jul 1, 2005, 01:40 AM
 
You could also check out OmniDiskSweeper - not quite what you want but it helps you drill in to find where your disk space went.

or use finder to search for files > xxxx Mb
(Last edited by Gavin; Jul 1, 2005 at 01:42 AM. (Reason:safari need big big grammar checker))
     
Mac Elite
Join Date: Jun 2000
Status: Offline
Reply With Quote
Jul 6, 2005, 10:01 AM
 
I back up my computer using the command prompt and before I start, I like to know how much data each directory in my home folder is using. I do this, I issue the following command:

Code:
du -h -d 0
The -h means human readable, so it will print sizes in megabytes, gigabytes, and so on. The -d 0 is the depth level, which allows you to control how far down it dig from the current directory. There is also a -c option for grand total but I have never used it.

Hope this helps.
Agent69
     
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
Jul 6, 2005, 10:13 AM
 
Originally Posted by Agent69
Code:
du -h -d 0
That is helpful, but what I'm really looking for is to find the largest twenty or thirty files on my machine sorted by size. Is there a way to do that?

There is a program called DooBar that might do the trick, but I'd prefer to do it via the command line if possible.
(Last edited by selowitch; Jul 6, 2005 at 10:23 AM. )
     
Moderator
Join Date: May 2001
Location: Hilbert space
Status: Offline
Reply With Quote
Jul 6, 2005, 10:41 AM
 
Code:
du -h -d 1
is more helpful. It'll show you how much space each subdirectory of the current directory takes.
I don't suffer from insanity, I enjoy every minute of it.
     
Mac Elite
Join Date: Nov 2003
Location: Rockville, MD
Status: Offline
Reply With Quote
Jul 6, 2005, 11:49 AM
 
Code:
du -h -d 1 *|sort
Better yet, I think. The only trouble is it sorts alphabetically, so "k" comes before "mb" and so it doesn't truly order by size. Any ideas how to further refine it?
     
Mac Elite
Join Date: Jun 2000
Status: Offline
Reply With Quote
Jul 6, 2005, 02:04 PM
 
Code:
du -h -d 1
is more helpful. It'll show you how much space each subdirectory of the current directory takes.
You're right. That's what I get for working for memory (I'm at work).

If you're wanting to sort by size, you would probably NOT want human readable output. It would probably be better to use the default or -k for consistancy's sake.

You could probably do something along the line of:

Code:
du -d 1 * | sort | head -n 30
However, when I tried this with a secure shell account on a OpenBSD machine, files with zero bytes showed up first, so you would probably need to prevent those lines from printing. (I believe you could use Grep to accomplish this but I am not sure.)
Agent69
     
Mac Elite
Join Date: Jun 2000
Status: Offline
Reply With Quote
Jul 6, 2005, 10:52 PM
 
Actually, it looks like I was remembering correctly about "-d 0". Here is a copy of what happens when I do so in my home directory:

Code:
Goot:~ goo$ du -h -d 0 * 181M Applications 121M Audio 16K Desktop 63M Documents 201M Library 291M Movies 121M Music 286M Pictures 582M Zoe Goot:~ goo$
Note that this command returns the sizes of the directories in Home. Here is what happens with a "-d 1":

Code:
Goot:~ goo$ du -h -d 1 * 244K Applications/Backdrop.app 7.2M Applications/BitTorrent.app 1.8M Applications/Bookdog.app 22M Applications/Camino.app 4.4M Applications/Cocktail.app 4.9M Applications/Cog.app 552K Applications/ExifRenamer.app 19M Applications/GraphicConverter 36M Applications/MacMAME 11M Applications/NetNewsWire Lite.app 1.7M Applications/NicePlayer.app 25M Applications/OmniOutliner Professional.app 23M Applications/RealPlayer.app 2.4M Applications/Safari Enhancer.app 156K Applications/SafariSpeed.app 732K Applications/SMARTReporter.app 10M Applications/SubEthaEdit.app 72K Applications/Twemco.wdgt 3.5M Applications/ViewIt.app 6.7M Applications/XinePlayer.app 181M Applications 65M Audio/Gwen Sefani - Love Angel Music Baby 56M Audio/Wang Chung - To Live And Die In LA 121M Audio 16K Desktop 36K Documents/Comic Books 68K Documents/Federal Taxes 7.9M Documents/Flash 632K Documents/Mac Weekly Journal 41M Documents/MacMAME 13M Documents/Miscellaneous 63M Documents 0B Library/Address Book Plug-Ins 216K Library/Application Enhancers 4.6M Library/Application Support 0B Library/Assistants 0B Library/Audio 0B Library/Autosave Information 191M Library/Caches 0B Library/Classic 0B Library/ColorPickers 4.0K Library/Colors 8.0K Library/ColorSync 0B Library/Contextual Menu Items 216K Library/Cookies 0B Library/Documentation 4.0K Library/Favorites 40K Library/FontCollections 680K Library/Fonts 36K Library/Icons 0B Library/iMovie 8.0K Library/Internet Plug-Ins 0B Library/iTunes 0B Library/Keyboard Layouts 32K Library/Keychains 236K Library/Logs 428K Library/Mail 0B Library/Mail Downloads 0B Library/Phones 468K Library/PreferencePanes 2.7M Library/Preferences 0B Library/Printers 440K Library/Receipts 4.0K Library/Recent Servers 32K Library/Safari 0B Library/Saved Searches 0B Library/Screen Savers 172K Library/Services 0B Library/Sounds 4.0K Library/Spelling 12K Library/Syndication 201M Library 291M Movies 121M Music/iTunes 121M Music 1012K Pictures/BeOS-Icons 3.5M Pictures/Cgchannel 38M Pictures/Deviantart 1.3M Pictures/FR4NK 396K Pictures/Frank-Cho 796K Pictures/Katrine-Ewert 13M Pictures/Mitch-Bird 6.5M Pictures/My Junk 159M Pictures/Photosig 3.0M Pictures/Photosig - REQs 24M Pictures/Rakaz.nl 4.2M Pictures/Rampant-Mac 23M Pictures/Screenshots 4.4M Pictures/Sxc.hu 3.2M Pictures/Wallpapers 286M Pictures 582M Zoe Goot:~ goo$
Agent69
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Jul 7, 2005, 05:51 PM
 
I use the following script to sort the output of du . by descending size, and convert the sizes to human-readable.
Use like this:
du | usagereport.pl all > usage.txt

#!/usr/bin/env perl
use warnings;
use strict;

my ($doall) = @ARGV;

my @dirs;

while (<STDIN>)
{
chomp;
if (m/^(\d+)\s+(.*)$/)
{
my ($size, $dir) = ($1, $2);
my $is_topdir = 0;
if ($dir =~ m/^(\.\/)?([^\/]*)$/)
{
$is_topdir = 1;
}

my %this_dir;
$this_dir{'dir'} = $dir;
$this_dir{'size'} = $size;
if ($doall)
{
push( @dirs, \%this_dir );
}
else
{
if ($is_topdir)
{
push( @dirs, \%this_dir );
}
}
}
}

# sort
@dirs = sort { $$b{'size'} <=> $$a{'size'} } @dirs;

#
# output
#
foreach my $dir_ref (@dirs)
{
my %this_dir = %$dir_ref;
print size_to_human($this_dir{'size'}) . "\t" . $this_dir{'dir'} . "\n";
}

sub size_to_human
{
my ($size) = @_;

my $prec = 2;

my $size_kb = $size;
my $size_mb = round( $size / (1024 **1), 2);
my $size_gb = round( $size / (1024 **2), 2);

my $size_text = "";
if ($size_gb > 1)
{
$size_text = "$size_gb GB";
}
elsif ($size_mb > 1)
{
$size_text = "$size_mb MB";
}
else
{
$size_text = "$size_kb kB";
}
}

sub round
{
my ($x, $prec) = @_;
return ( (int ( $x * (10 ** $prec) ) ) / (10 ** $prec) );
}
Hope that helps.
-mithras
     
Fresh-Faced Recruit
Join Date: Aug 2005
Status: Offline
Reply With Quote
Aug 12, 2005, 06:02 AM
 
Originally Posted by selowitch
Code:
du -h -d 1 *|sort
Better yet, I think. The only trouble is it sorts alphabetically, so "k" comes before "mb" and so it doesn't truly order by size. Any ideas how to further refine it?
The following commandline will give you disk usage in kilobytes for any item (file or dir) below the current dir. The results are then sorted numerically (-n) and in reverse (-r) so largest files are reported first. Finally the 10 first on the list are shown
Code:
du -k . | sort -nr | head -10
Listing everything in kilobytes (du -k) instead of in human readable format (du -h) makes sorting more relevant
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2