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

Use a shell wrapper to make Mac du like GNU du UNIX
This small shell script will turn the default Mac du into the more user-friendly GNU du -h variant. The -h flag stands for "human readable" and instead of du telling you that a given folder uses "4628" 1024-byte blocks, you'll see that it uses "4.6M" (for Megabytes). To add this human readable format to the existing du, make a shell script called duh.sh with the following code:
 # Make du -k into du -h 
 # 
 du -k $1 $2 $3 $4 | awk '{if($1>1024){r=$1%1024;if(r!=0)
   {sz=($1-r)/1024}else{sz=$1/1024}print sz"Mt"$2;}
   else{print $1"Kt"$2}}' 
 #
 # End Code
The three lines starting with du -k $1 $2... should be entered as one long line with no additional spaces other than what's already shown. Don't forget to set the perms to execute by typing chmod a+x duh.sh. Now you can just use duh.sh in place of du. To make life even better, you can alias duh.sh to du to replace the stock command:
 % alias du=`duh.sh`
[robg adds: I wrote a hint a couple of years back that explained how to install a subset of the GNU disk utilities, including the humand-readable forms of both du and df. This hint accomplishes much the same result without installing any new software.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[6,225 views]  

Use a shell wrapper to make Mac du like GNU du | 5 comments | Create New Account
Click here to return to the 'Use a shell wrapper to make Mac du like GNU du' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
messed up characters
Authored by: Krioni on Jul 02, '03 12:39:49PM
Probably would have been a good idea to use the code tags around that. Where it says Mt, it should be:
M\t
and Kt should be
K\t


[ Reply to This | # ]
one more thing
Authored by: Krioni on Jul 02, '03 12:42:14PM

I would think it could be a potential problem to alias du right to your shell script. What if some other tool expects the current format? Better to just name the shell script duh, and call it directly when you want it. Of course, it's "do as you please, at your own risk." :-)



[ Reply to This | # ]
one more thing
Authored by: mervTormel on Jul 02, '03 01:28:02PM

yep. aliasing a true command can lead to all sorts of headaches, especially discovering and solving the issue long after you've forgotten that you've done it ;]



[ Reply to This | # ]
Bite the bullet :)
Authored by: melo on Jul 02, '03 03:18:10PM

You can also bite the bullet, start up FinkCommander, and install the fileutils package.

All the glory of GNU du and df with humam-friendly formats for you...



[ Reply to This | # ]
"$BLOCKSIZE" Environment Variable
Authored by: OneTrueDabe on Jul 02, '03 09:52:17PM

Note that the Mac OS X Unix utilities honor the "BLOCKSIZE" environment variable. Setting this to "1k" will effect not only "du", but also "df" (for filesystem usage) and the "-s" flag to "ls" ("ls -s" normally shows the number of 512-byte blocks, which is twice the number of kilobytes[*]) as well as any other applications which support it.

In tcsh:

setenv BLOCKSIZE 1k

... in bash:

export BLOCKSIZE=1k

... to get consistent output across "du", "df", and "ls -s".

* - The numbers reported in the first column of "ls -s" may be different from the size reported when you don't use the "-s" flag.

In the case of some Quicktime movies, for example, the size reported is 2GB, but the movie file itself only takes up a few hundred megabytes on disk. This is called a "sparse file", and simply allows the programmer to create a huge empty file ahead of time, then "fill it in" as she goes.



[ Reply to This | # ]