0

Mac Terminal df command is a tool I use to get disk space. For example, before I get a new software or update, I also take a note of my HD space before and after the installation.

I remember I used this command with -b, -k, -m or -g options to get size in 512-bytes-block, kilobytes, megabytes or gigabytes. According to man (manual description) of df, those options may change BLOCKSIZE as well so that if I use "df -h" or "df -H" afterwards, the result will be a size in either binary base or decimal base. I remember that tried this before and it worked fine.

Recently, I try to use "df -b" to reset BLOCKSIZE. I then try "df -h" or "df -H" again, I get size values all as gigabytes (binary or decimal), not expected in 512-bytes. Then I try to use "df -hb" or "df -Hb", the result values are the same(512-bytes-block).

Here are some examples I had before:

$ df -k          # this command I get the expected result: 1024-block
Filesystem     512-blocks       Used     Available ...
/dev/disk1s2    97101344    403566296    572023048 ...
....

$ df -h         # the result in binary base, value in 1024-block
Filesystem     1024-blocks       Used      Avail ...
/dev/disk1s2    97101344    403566296  572023048 ...
....

$ df -H         # the result in decimal base, value in 1,000 base
Filesystem     Size         Used     Avail ...
/dev/disk1s2 99431776  413251887 585751601 ...
....

As the above example, "df -k" resets the BLOSKSIZE to kilobytes. However now I repeat thes same commands, but I cannot get the same results:

$ df -k          # this command I get the expected result: 1024-block
Filesystem     512-blocks       Used         Avail ...
/dev/disk1s2    97101344    403566296    572023048 ...
...

$ df -h         # the result in binary base but values in Gigabytes
Filesystem     Size    Used  Avail ...
/dev/disk1s2   465Gi  192Gi  273Gi ...
....

$ df -H         # the result in decimal base by values in Gigabytes
Filesystem     Size    Used  Avail ...
/dev/disk1s2   500G    207G   293G ...
....

I am not sure what prevent the BLOCKSIZE value not being reset. As a result, I cannot get the size in decimal base (1000 for Kilobytes in decimal base, 1,000,000 for Megabytes in decimal base, ...).

By the way, I get the information about usage of df and BLOCKSIZE from:

$ man df
1
  • I edited the question to make my issue clear. Sep 25, 2009 at 14:36

2 Answers 2

1

I think your memory of how the command used to work is wrong; df has never changed BLOCKSIZE, only overridden it temporarily. In fact, it cannot change BLOCKSIZE for anything but itself -- if you run df 3 times, each instance gets its own copy of your shell's environment, and can modify its own copy, but can't do anything to the shell's environment (or the other instances' either).

That said, I think the only way to get the behavior you want is to actually change BLOCKSIZE in the shell's environment:

export BLOCKSIZE=1000000   # Base-10 Megabytes
df   # No flags, so it uses the inherited BLOCKSIZE

Note that this way of doing things gives the persistent behavior you describe; after setting BLOCKSIZE for the parent shell, it's passed to all commands you run in that shell from that point on (until/unless you change it again). If you only want to change it for a particular command, use this instead:

BLOCKSIZE=1000000 df   # Displays in base-10 Megabytes
df   # Displays in the default format
2
  • Is there any way to see the value of BLOCKSIZE. I tried to type set in terminal and I don't see this value in the environment list. Sep 25, 2009 at 19:33
  • echo $BLOCKSIZE will display its value, if there is an environment variable by that name set (you can also use printenv to show all environment variables. Normally, it's not likely to be set, in which case df uses its default of 512-byte blocks. Actually, let me clarify that: if a unit is specified with a command-line flag (like -k or -h), df uses that; otherwise it looks for BLOCKSIZE, and if it's set uses that; otherwise it uses 512-byte blocks. Sep 26, 2009 at 23:50
0

This should use default 512 byte blocks:

df -P

If using human readable format, once the amount shown is enough for the next measurement it will display in that (eg. 1024M -> 1G)

6
  • how about size value in decimal base? This option only lists size in binary base Sep 25, 2009 at 4:48
  • I think that --block.. is a comment, right. It should be #...? Sep 25, 2009 at 4:50
  • See my question, actually, the result of "df -h" lists values in Gi at my Mac. Sep 25, 2009 at 4:51
  • BSD df always confuses me. df -P -H seems closer to what you're looking for. -P specifies the default 512 byte blocks and -H is human readable for base 10 sizes.
    – user1931
    Sep 25, 2009 at 5:00
  • As I mentioned that, -H does display values in G unit instead of the default 512-bytes. -P does display values in 512-block. However, how about k or m units in decimal base? I think somehow, df -k or -m does not overwrite BLOCKSIZE in my Mac anymore. Therefore, I cannot use -H to get values in decimal base in k or m unit. Sep 25, 2009 at 5:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .