Get free disk space with df to just display free space in kb?

后端 未结 4 1127
谎友^
谎友^ 2020-12-13 13:35

I\'m trying to output the amount of free disk space on the filesystem /example.

If I run the command df -k /example I can get good informat

4条回答
  •  借酒劲吻你
    2020-12-13 14:00

    You can use stat(2) command to display free blocks and also to find out how large each block is, e.g.

    stat -f --printf="%a %s\n" /
    

    will display number of free blocks (%a) on a given file system (/) followed by a block size (%s). To get size in kB, you can use bc(1) command as in the following example:

    stat -f --printf="%a * %s / 1024\n" / | bc
    

    Finally, to put this into a variable is just a matter of using backtick substitution (or $() as in the first answer):

    SOMEVAR=`stat -f --printf="%a * %s / 1024\n" / | bc`
    

提交回复
热议问题