image
image

Go Back   macosx.com > Mac Help Forums > HOWTO & FAQs

Reply
 
LinkBack Thread Tools
  #1  
Old December 10th, 2002, 01:49 AM
~departed~
 
Join Date: Jul 2002
Location: Kansas City, MO
Posts: 457
Thanks: 0
Thanked 0 Times in 0 Posts
gatorparrots is on a distinguished road
How to find disk usage by file type

Say you want to find out how big your MP3 collection is. It's relatively easy to find all the .mp3 files on a drive and calculate the total amount of disk space they collectively occupy, all from one command.

We want to run a find command, then pipe the output to du via xargs. (The -0 flag for xargs in conjunction with the -print0 flag for find will handle spaces in filenames. From the xargs man page:
Code:
-0          Use NUL (``\0'') instead of whitespace as the argument sepa-
                 rator.  This can be used in conjuction with the -print0
                 option of find(1).
Here is the command:
sudo find / -iname "*.mp3" -print0 | xargs -0 du -cks

To make it more fun, throw an alias into your .*shrc file like so:
alias diskspc 'sudo find / -iname "*\!:1*" -print0 | xargs -0 du -cks'

This will net you the ability to search for any file type. For example, issuing diskspc .mp3 would return the filesize for every MP3 file (regardless if its extension is .MP3 or .mp3, thanks to the -iname case insensitive flag for find).

Last edited by gatorparrots; December 10th, 2002 at 02:20 AM.
Reply With Quote
  #2  
Old December 10th, 2002, 04:17 PM
Registered User
 
Join Date: Aug 2002
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
DMCrimson is on a distinguished road
kewl, thanks!
__________________
Sawtooth G4 @ 1.4GHz, Club3d Radeon 8500LE (full 8500 when flashed), Pioneer DVR-A05
Reply With Quote
  #3  
Old December 12th, 2002, 01:29 AM
Writer, et. al.
 
Join Date: Aug 2002
Location: Boulder, CO
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
d1taylor is on a distinguished road
Or... another way to solve this problem

Code:
find / -type f -name "*.mp3" -ls | awk '{ sum += $7 } END { print "total size: " sum }'
This exploits the -ls output of find and lets us sidestep the problem of worrying about filenames with spaces: field seven is the size of the file in bytes, and the awk snippet sums up all the matching lines.

When I run this on my disk, I see a result of total size: 24009565121

Do the math and you'll see that, correctly, I have 22.3 GB of music files online.
__________________
Author, Learning Unix for Mac OS X (O'Reilly), among other books.
Check out http://books.intuitive.com/
Reply With Quote
  #4  
Old December 12th, 2002, 01:48 AM
~departed~
 
Join Date: Jul 2002
Location: Kansas City, MO
Posts: 457
Thanks: 0
Thanked 0 Times in 0 Posts
gatorparrots is on a distinguished road
In either method, the command should be run as root if you wish to avoid access errors and ensure a more complete search.

d1taylor's method took @1.75 minutes to scan 160GB of data and locate 156M total of MP3 files on my three hard disks. My method took just over 1 minute to complete. (Testing was done informally without a stopwatch or multiple runs. Drive caches and other factors may have influenced the results, but overall the method I posted seems more effecient.)

Another advantage of using my method and setting up the alias is that you can find ALL instances of a particular file type by extension, regardless of its case. For example, to find all tiff files you would issue: diskspc .tif and my method will return the disk space for all files with .tif, .TIF, .TIFF, or .tiff extensions.

A slight revision
If you have fileutils installed, you can change the flag on du from -k (kilobytes) to -h (human readable), so it will return the result in megabyes instead of kilobytes.

The revised code is then:
sudo find / -iname "*.mp3" -print0 | xargs -0 du -ckh

The alias line:
alias diskspc 'sudo find / -iname "*\!:1*" -print0 | xargs -0 du -ckh'

Last edited by gatorparrots; December 12th, 2002 at 02:11 AM.
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting 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 Off
Pingbacks are Off
Refbacks are Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The Authoritative Partitioning Discussion mr. k Mac OS X System & Mac Software 15 January 9th, 2009 08:19 AM
[HOWTO] - Find files of same type or creator as another file jverd Mac OS X System & Mac Software 3 March 19th, 2003 07:05 PM
10.22 and CD Mounting emh_alpha1 Mac OS X System & Mac Software 5 December 5th, 2002 04:13 PM
I installed Fink under root and..... Hydroglow Unix & X11 5 November 27th, 2002 04:57 PM
vignette client on mac os x erim Software Programming & Web Scripting 8 July 13th, 2001 02:14 PM


All times are GMT -5. The time now is 08:47 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC1
Copyright 2000-2010 DigitalCrowd, Inc.