4

I am writing a geektool 3 script to show the size of a particular VMware Fusion virtual machine. Such .vmwarevm "file" is really a packaged directory.

Get Info in Finder says the file is "52.91 GB". I run the following du command to get the file size:

> du -hs ~/Documents/Virtual\ Machines.localized/MY-PRECIOUS-7.vmwarevm | awk '{print $1}'

This du -hs command returns the file size as "49G". What accounts for the difference from what Finder reports?

Alternatively, I have tried replacing the -s option with the -d option like so:

du -hd ~/Documents/Virtual\ Machines.localized/MY-PRECIOUS-7.vmwarevm | awk '{print $1}'

This du -hd command returns the file size as "59G". What accounts for the difference between Finder, du -hd, and du -hs?

Also, this du -hd command produces no output in geektool 3. What gives?

Arjan
  • 31,511
flipdoubt
  • 257

4 Answers4

7

Not an Mac OS X user but I read somewhere that Finder uses base-10 nowadays.

Could the difference be that du still uses base-2?

49.0 GiB * (1024^3 bytes / GB) = 52,613,349,376 bytes = 52.6 GB

(the small difference is because du is rounding to the nearest GB)

Nifle
  • 34,998
5

Also note that "du" returns the actual disk space used, whereas other tools will return the allocated space. Where you have sparse files these 2 values can be quite different - I have a file that has 2 TB allocated to it but it is only occupying about 200 MB of disk space.

Cry Havok
  • 3,566
3

Nifle is correct. du returns base-2, Finder returns base-10, and 49GiB ≈ 52.91GB

The -d argument requires you to specify a depth. From experimenting just now, it appears if you give something besides a number for depth, it eats the argument you specified and behaves as if you did -d 0. The 59G you're getting is the size of the current directory, not the size of the vmwarevm file.

Here is the experiment I ran:

[~]$ ls -d Code
Code/

[~]$ du -h -s Code
8.3M    Code

[~]$ du -h -d 0 Code
8.3M    Code

[~]$ du -h -d Code
 38G    .           <-- this is the size of ~/

[~]$ du -h -d 0
 38G    .

You might want to read the Mac OS X du(1) manpage for more information.

0

If you need the size equal to Finder's size, you can use stat

stat -f%z path/to/file

It returns the size in bytes, so you can divide by 1000000 to get the MB size. Here is the full command (the scale sets the number of numbers after the decimal point in the result)

echo "scale=2; $(stat -f%z path/to/file) / 1000000" | bc