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

Another means of getting a disk usage summary System
Have you ever wanted to know which of the subdirectories in your home folder is consuming 5 gigabytes of your hard drive for no clear reason? I did, and suffered through clicking on the Get Info menu option about 10 times before I threw up my hands and tried using the Terminal's du comamnd. I was sorting through the 3,000 lines of its output, wishing for a summary, and wrote this script.

Yes, it is a shell script. Yes, you have to run a terminal window to use it. No, your hands won't have hairy palms when you're done. But you'll be able to view top level summaries in any directory with trivial ease, and track down the big disk hogs.

The script works by piping the output of the du command into a very short awk script, which strips out all bits of info regarding nested subdirectories, and converts the remaining output lines into a consistent, human readable format for display. Copy thsi script to a file (I named mine /usr/local/bin/dux) and make it executable (chmod +x dux). You can aslo download a copy from my website.
#!/bin/sh

# a shell script to print directory size summaries
# rick vannorman 20oct2004
# sub@neverslow.com

du -ck | awk '{
   dir = $0;                # preserve the input recordd
   x = gsub(/\//,"",dir);   # count the slashes, nesting level
   if (x != 1) next;        # ignore all but top level
   size = $1 / 1024;        # convert 1024k block count to meg
   $1 = "";                 # blank the input field
   printf("%12.3fM %s\n",size,$0);  # display user friendly output
}'
[robg adds: There are a number of other disk usage hints here -- 1, 2, 3, 4 -- each works in a somewhat different manner, hence they're not quite exact duplicates. Between this hint and the others, you should be able to find a disk space tool that you like! I like this script, as it (a) shows the hidden directories' sizes, and (b) it's quicker than a du -ksh, which has been my preferred method.]
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[17,495 views]  

Another means of getting a disk usage summary | 25 comments | Create New Account
Click here to return to the 'Another means of getting a disk usage summary' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Another means of getting a disk usage summary
Authored by: CaptCosmic on Oct 21, '04 11:29:29AM

Or you could just use the '-s' flag to the du command.

'du -shk *' will give you a list of all the directories and files in the current directory along with their sizes in human readable format.

---
Capt Cosmic



[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: Nabby on Oct 21, '04 05:04:53PM

Actually, they give slightly different outputs. I guess it depends on your needs. The script will give you hidden directories and not show you files in the current directory. The command by itself, with the -s option, will omit the hidden directories, but give you the size of the files in your current directory. Neither appears to give the size of hidden files in the current directory. As I said, it depends on your needs as to which works best. I like the scirpt, as I was able to quickly port it to my AIX boxes at work, and they don't have the whole "human readable" option down, so I like the formating of the scirpt output.



[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: Jahnoth on Oct 21, '04 11:35:03AM

I use WhatSize (http://www.macupdate.com/info.php/id/13006). It will fairly quickly measure any drive you want, and it's free!



[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: mathieul on Oct 21, '04 12:40:44PM
Thanks, it's a great tool! MacUpdate has an old version of the software (10.2.4) and a wrong URL for the developper website, so you should go to http://www.id-design.com/software/whatsize/index.php if you want the latest version (10.2.5).

[ Reply to This | # ]
Disk Inventory X
Authored by: jfranusic on Oct 21, '04 11:50:48AM

I have been using an impressive utility called "Disk Inventory X". This software will scan a folder and catagorize files by type, the real power of Disk Inventory X is in how the information is displayed, files are displayed in a two dimentional grid, the size of a square on the grid is proportional to the size on disk.

It is very easy to pick out large files or folders using this tool.
(All that and it is open source!)

http://www.derlien.com/diskinventoryx/



[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: fds on Oct 21, '04 11:52:53AM

What's the difference between this and

du -hd1



[ Reply to This | # ]
du -hd1
Authored by: ssevenup on Oct 21, '04 06:34:20PM

Other than that it doesn't work? Or did you mean "du -d --max-depth=1"


---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
du -hd1
Authored by: ssevenup on Oct 21, '04 06:36:29PM

Actually I meant "du --max-depth=1 -h"

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
du -hd1
Authored by: fds on Oct 21, '04 10:50:04PM

Actually, the BSD version of du included with Mac OS X doesn't even understand the long --max-depth form. That's a GNU fileutils du thing.

On checking the URL of the site, I see MacOSXHints and not LinuxHints.
(Although Fink and similar might also kindly mess up your system and install GNU fileutils when you don't want it.)



[ Reply to This | # ]
du -hd1
Authored by: fds on Oct 21, '04 10:44:13PM

Of course it does work - in Mac OS X Panther 10.3.5, that is.

Is the shorthand form a new feature compared to earlier releases? I doubt that. Perhaps you are using a different version of "du" than the one installed by Apple.

Check that
command -V du
points to /usr/bin/du.



[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: lpangelrob on Oct 21, '04 11:55:56AM

If you have Konfabulator, the Magic Space Bus is a great way to access similar information.

http://www.widgetgallery.com/view.php?widget=35776

---
-Robert Guico



[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: alset on Oct 21, '04 01:24:21PM
Obligatory mention of OmniDiskSweeper.

---

- - = = = standing on the shoulders of giants = = = - -

[ Reply to This | # ]

jDiskReport
Authored by: bozonator on Oct 21, '04 01:28:33PM
Another option is the Java application jDiskReport from http://www.jgoodies.com/freeware/jdiskreport/

It's not bad.

-- Bozonator

[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: aptenergy on Oct 21, '04 02:13:19PM

I use

cd /; sudo du -k | sort -nr > ~/Desktop/diskusage.txt
(enter password)

It presents me with the biggest folders and allows me to go through them. You can always add additional grep arguments if you want to filter out things like the System folder and etc.



[ Reply to This | # ]
Save a few trips to the menu...
Authored by: Dolomite on Oct 21, '04 03:19:41PM

A little known Finder feature: CMD-OPTION-I or Option-Click File menu and Get Info becomes File Inspector. One window updates on which file is selected.



[ Reply to This | # ]
Save a few trips to the menu...
Authored by: aqsalter on Oct 21, '04 09:53:27PM

Could you please post this as a hint? I wondered where this went ;)



[ Reply to This | # ]
Save a few trips to the menu...
Authored by: ChaChi on Oct 22, '04 02:15:11PM
The "file inspector" has already been a hint:
See here.

[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: maniabug on Oct 21, '04 06:39:03PM

du -ck | sort -n | tail
might be the sweet spot between short and useful. It's even memorable. Duck, sort, and tail. Kind of like stop, drop, and roll. Or duck and cover.



[ Reply to This | # ]
a little modification
Authored by: fmarmar on Oct 21, '04 09:55:49PM
modifying a little the script you can use the power of -h option. Here are my personal version of script.

#!/bin/sh

# a shell script to print directory size summaries
# rick vannorman 20oct2004
# sub@neverslow.com
# modified by Felix Martin 22oct2004

du -ckh | awk '{
   dir = $0;                # preserve the input recordd
   x = gsub(/\//,"",dir);   # count the slashes, nesting level
   if (x != 1) next;        # ignore all but top level
   size = $1;        # convert 1024k block count to meg
   $1 = "";                 # blank the input field
   printf("\t%s\t%s\n",size,$0);  # display user friendly output
}'


[ Reply to This | # ]
a little modification
Authored by: Xeo on Oct 22, '04 12:55:20AM

As others have said, there is no reason to use this script when du has the ability to do this on it's own. Having it print out EVERY file only to filter it with awk is a waste of processing time.



[ Reply to This | # ]
Another means of getting a disk usage summary
Authored by: MartySells on Oct 22, '04 12:38:47AM
I like this one. Adding | sort +0n on the end will show largest things at the bottom of the output. du -hcd 1 seems very close to what this tip does but also includes a total at the bottom.

Rob forgot to mention one I submitted - Create HTML disk usage trees from du output. With it you can run one "du -k" into an output file and then render multiple different trees without the overhead of doing a new du for each tree - it's a little higher level than the script-bits discussed here.

Regarding different versions of du and what options they support:
  • -k and -h are mutually exclusive. If you specify both the -k has no meaning.
  • Some versions (GNU) let you specify -H or --si which are like -h, but use powers of 1000 not 1024.
  • The OS X df command supports both -h and -H in this way but the OS X du command only supports -h (-H on the OS X version has a different use)
-m

[ Reply to This | # ]
even simpler
Authored by: karnat10 on Oct 22, '04 03:19:09AM
I have this in my ~/.bash_profile:
alias dus='du -hcs'
now 'dus' does the same thing

[ Reply to This | # ]
Er, what's wrong with just using the finder?
Authored by: ptwithy on Oct 22, '04 10:55:55AM

I must be missing something. If I open a Finder window for my hard drive, select list view, sort by size, and turn on View/Show View Options/Calculate all sizes, I get a nice sorted list of folders by size. Toggling the 'disclosure triangle' on the largest folder will show me the sizes of the folders in it. Using this technique I can easily explore my drive looking for space hogs.

Unfortunately, 90% of my disk is hogged by (legally purchased) music, which I have no intention of deleting. ;)

[Turn off Calculate All Sizes when you are done to save wear and tear.]



[ Reply to This | # ]
Er, what's wrong with just using the finder?
Authored by: demmons65 on Oct 22, '04 12:33:32PM

Nothing wrong with Finder's way of giving this info.

It's just not in the terminal. So this hint (and others mentioned here) gives the command-line user an alternate to hiding the terminal, opening a window, navigating to the appropriate folder, toggling Calculate Sizes, etc..

It will also work if you're using a terminal in X Windows. And it will let you view sizes on 'hidden' directories that normally do not appear in Finder.

Many find the command-line method faster, and dare I say it, easier.

---
d a v e

http://www.hostwerks.com/~dave/



[ Reply to This | # ]
Er, what's wrong with just using the finder?
Authored by: starwxrwx on Oct 25, '04 10:53:48PM

well actually the hint author seems to have reverted to the command line when the gui options wern't working for them

but the list view "calculate sizes" is great in the finder - I had never tried that one...

I would suggest the hint author hit the man page for du... though, I have to admit I have dupes of lots of command line tools and man pages via fink, so I dont know how useful the apple ver is



[ Reply to This | # ]