How can I *only* get the number of bytes available on a disk in bash?

≡放荡痞女 提交于 2019-11-29 14:51:06

Portably:

df -P /dev/sda1 | awk 'NR==2 {print $4}'

The -P option ensures that df will print output in the expected format, and will in particular not break the line after the device name even if it's long. Passing the device name as an argument to df removes any danger from parsing, such as getting information for /dev/sda10 when you're querying /dev/sda1. df -P just prints two lines, the header line (which you ignore) and the one data line where you print the desired column.

There is a risk that df will display a device name containing spaces, for example if the volume is mounted by name and the name contain spaces, or for an NFS volume whose remote mount point contains spaces. In this case, there's no fully portable way to parse the output of df. If you're confident that df will display the exact device name you pass to it (this isn't always the case), you can strip it:

df -P -- "$device" | awk -vn=${#device} 'NR==2 {$0 = substr($0, n+1); print $3}'

You may use awk,

df | awk '$1=="/dev/sda"{print $4}'

You can use an awk

df | grep sda | awk '{print $4}'
Alex Mantaut

Only in Linux

df --output=avail

You can query disk status with stat as well. To query free blocks on filesystem mounted at /:

stat -f -c '%f' /

To get the result in bytes instead of blocks, you can use shell's arithmetic:

echo $((`stat -f -c '%f*%S' /`))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!