bash 我怎样才能*只*获得bash中磁盘上可用的字节数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33249591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:47:03  来源:igfitidea点击:

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

linuxbashshellunixdf

提问by Ryan

dfdoes a great job for an overview. But what if I want to set a variable in a shell script to the number of bytes available on a disk?

df概述做得很好。但是,如果我想将 shell 脚本中的变量设置为磁盘上可用的字节数怎么办?

Example:

例子:

$ df
Filesystem            1K-blocks     Used Available Use% Mounted on
/dev/sda             1111111111  2222222  33333333  10% /
tmpfs                  44444444      555  66666666   1% /dev/shm

But I just want to return 33333333(bytes available on /), not the whole dfoutput.

但我只想返回33333333(可用字节数/),而不是整个df输出。

采纳答案by Gilles 'SO- stop being evil'

Portably:

便携:

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

The -Poption ensures that dfwill 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 dfremoves any danger from parsing, such as getting information for /dev/sda10when you're querying /dev/sda1. df -Pjust prints two lines, the header line (which you ignore) and the one data line where you print the desired column.

-P选项可确保df以预期格式打印输出,特别是即使设备名称很长也不会断开设备名称后面的行。将设备名称作为参数传递以df消除解析过程中的任何危险,例如获取/dev/sda10查询时的信息/dev/sda1df -P只打印两行,标题行(您忽略)和打印所需列的一行数据。

There is a risk that dfwill 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 dfwill display the exact device name you pass to it (this isn't always the case), you can strip it:

存在df显示包含空格的设备名称的风险,例如,如果卷是按名称挂载并且名称包含空格,或者对于远程挂载点包含空格的 NFS 卷。在这种情况下,没有完全可移植的方式来解析df. 如果您确信它df会显示您传递给它的确切设备名称(并非总是如此),您可以将其剥离:

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

回答by Avinash Raj

You may use awk,

你可以使用awk,

df --output=avail

回答by ergonaut

You can use an awk

你可以使用 awk

stat -f -c '%f' /

回答by Alex Mantaut

Only in Linux

仅在 Linux 中

echo $((`stat -f -c '%f*%S' /`))

回答by Zouppen

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

您也可以查询磁盘状态stat。要查询挂载于 的文件系统上的空闲块/

df -P --total | grep 'total' | awk '{print }'

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

要获得以字节而不是块为单位的结果,您可以使用 shell 的算法:

##代码##

回答by Odondon Labama

You may get exact number of bytes with df:

您可以使用 df 获得确切的字节数:

df -B1 / Filesystem 1B-blocks Used Available Use% Mounted on /dev/mapper/cl-root 32893632512 13080072192 18119061504 42% /

df -B1 / Filesystem 1B-blocks Used Available Use% Mounted on /dev/mapper/cl-root 32893632512 13080072192 18119061504 42% /

回答by SpicestMemeLord

Very similar answers to the ones shown, but if you don't know the name of filesystem and are just looking for the total available space on the partition.

与显示的答案非常相似,但如果您不知道文件系统的名称并且只是在寻找分区上的总可用空间。

##代码##